为什么我在 Python 中使用 BeautifulSoup 得到“'ResultSet'没有属性'findAll'”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/992183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting "'ResultSet' has no attribute 'findAll'" using BeautifulSoup in Python?
提问by Alex
So I am learning Python slowly, and am trying to make a simple function that will draw data from the high scores page of an online game. This is someone else's code that i rewrote into one function (which might be the problem), but I am getting this error. Here is the code:
所以我在慢慢地学习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'
Thanks in advance.
提前致谢。
回答by mechanical_meat
Wow. Triptych provided a greatanswerto a related question.
哇。Triptych 为相关问题提供了很好的答案。
We can see, from BeautifulSoup's source code, that ResultSet
subclasses list
.
我们可以看到,从BeautifulSoup的源代码,该ResultSet
子类list
。
In your example, get_rows
is an instance of BS's ResultSet
class,
and since BS's ResultSet
subclasses list
, that means get_rows is a list.
在您的示例中,get_rows
是 BSResultSet
类的一个实例,
并且由于 BS 的ResultSet
subclasses list
,这意味着get_rows 是一个 list。
get_rows
, as an instance of ResultSet
, does nothave a findAll
method implemented; hence your error.
What Triptych has done differently is to iterateover that list.
Triptych's method works because the items in the get_rows
list are instances of BS's Tag class; which has a findAll
method.
get_rows
作为实例ResultSet
,也没有有findAll
方法来实现; 因此你的错误。
Triptych 所做的不同之处在于迭代该列表。
Triptych 的方法有效,因为get_rows
列表中的项目是 BS 的 Tag 类的实例;其中有一个findAll
方法。
So, to fix your code, you could replace the last three lines of your create
method with something like this:
因此,要修复您的代码,您可以将方法的最后三行替换为create
以下内容:
for row in get_rows:
text = ''.join(row.findAll(text=True))
data = text.strip()
print data
Note to Leonard Richardson: in no way do I intend to demean the quality of your work by referring to it as BS ;-)
伦纳德·理查森 (Leonard Richardson) 的注意事项:我绝不打算通过将其称为 BS 来贬低您的工作质量;-)