所以我正在慢慢学习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'
提前致谢。
最佳答案
哇。 Triptych 提供了 great answer一个相关的问题。
我们可以看到,from BeautifulSoup's source code , ResultSet
子类 list
.
在您的示例中,get_rows
是 BS 的 ResultSet
类的一个实例,
由于 BS 的 ResultSet
是 list
的子类,这意味着 get_rows 是一个列表。
get_rows
,作为 ResultSet
的一个实例,没有实现了一个findAll
方法;因此你的错误。
Triptych 的不同之处在于对该列表迭代。
Triptych 的方法之所以有效,是因为 get_rows
列表中的项目是 BS 的 Tag 类的实例;它有一个 findAll
方法。
因此,要修复您的代码,您可以将 create
方法的最后三行替换为如下内容:
for row in get_rows:
text = ''.join(row.findAll(text=True))
data = text.strip()
print data
Leonard Richardson 的注意事项:我绝不打算通过称其为 BS 来贬低您的工作质量;-)
关于python - 为什么我得到 "' ResultSet' has no attribute 'findAll'“在 Python 中使用 BeautifulSoup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/992183/