我为什么得到"'结果'没有属性'findAll'"使用BeautifulSoup在蟒蛇?
-
13-09-2019 - |
题
因此,我的学习Python缓慢,并且我想要做一个简单的功能,将绘制数据的高分页的一个网络游戏。这是他人的代码,我改写成一个功能(这可能是问题),但是我得到这一错误。这里是代码:
>>> from urllib2 import urlopen
>>> from BeautifulSoup import BeautifulSoup
>>> def create(el):
source = urlopen(el).read()
soup = BeautifulSoup(source)
get_table = soup.find('table', {'id':'mini_player'})
get_rows = get_table.findAll('tr')
text = ''.join(get_rows.findAll(text=True))
data = text.strip()
return data
>>> create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
create('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
File "<pyshell#17>", line 6, in create
text = ''.join(get_rows.findAll(text=True))
AttributeError: 'ResultSet' object has no attribute 'findAll'
在此先感谢。
解决方案
哇。三提供了一个 伟大的 答案 一个相关的问题。
我们可以看到, 从BeautifulSoup的源代码, 那 ResultSet
子类 list
.
在你的实例, get_rows
是的一个实例BS的 ResultSet
类,
自BS的 ResultSet
子类 list
, 那意味着 get_rows是一个列表.
get_rows
, ,作为一个实例 ResultSet
, 不 不 有一个 findAll
方法实施;因此,你的错误。
什么三已经做了不同的是 迭代 在该列表中。
三相关的方法工作,因为该项目在 get_rows
列表情况的BS的标签等级;其中有一个 findAll
法。
因此,要解决你的代码,你可以替换的最后三行的你 create
方法与这样的事情:
for row in get_rows:
text = ''.join(row.findAll(text=True))
data = text.strip()
print data
注意到伦纳德*理查森:在没有办法做我想贬低你的工作质量,通过参照它作为BS;-)
不隶属于 StackOverflow