为什么我会收到"“结果”有没有属性'的findAll'"使用BeautifulSoup在Python? [英] Why am I getting "'ResultSet' has no attribute 'findAll'" using BeautifulSoup in Python?
问题描述
所以我学习Python的慢,我试图做一个简单的功能,将借鉴网络游戏的高分页面数据。这是别人的code,我改写成一个功能(这可能是问题),但我得到这个错误。这里是code:
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:
>>> 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'
先谢谢了。
推荐答案
哇。三联提供的<一个href=\"http://stackoverflow.com/questions/989872/how-do-i-draw-out-specific-data-from-an-opened-url-in-python-using-urllib2/989920#989920\">great回答以一个相关的问题。
Wow. Triptych provided a great answer to a related question.
我们可以看到,<一个href=\"http://$c$c.google.com/p/google-blog-converters-appengine/source/browse/trunk/lib/BeautifulSoup.py\">from BeautifulSoup的源$ C $ C ,即的ResultSet
子类的 列表
。
We can see, from BeautifulSoup's source code, that ResultSet
subclasses list
.
在你的榜样, get_rows
是BS的的ResultSet
类的一个实例,
并且因为BS的的ResultSet
子列表
,即用于 get_rows是一个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
,因为的ResultSet
的一个实例,做的不可以有一个的findAll
方法来实现;因此,您的错误。
三联什么做不同的是为迭代在该列表中。
三联的方法的工作,因为在 get_rows
列表中的项目是BS的标签类的实例;其中有一个的findAll
方法。
get_rows
, as an instance of ResultSet
, does not have a findAll
method implemented; hence your error.
What Triptych has done differently is to iterate over 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.
因此,要解决您的code,你可以替换最后三行的创建
法是这样的:
So, to fix your code, you could replace the last three lines of your create
method with something like this:
for row in get_rows:
text = ''.join(row.findAll(text=True))
data = text.strip()
print data
请注意伦纳德·理查森:绝不做我打算将其称为BS贬低你的工作质量; - )
Note to Leonard Richardson: in no way do I intend to demean the quality of your work by referring to it as BS ;-)
这篇关于为什么我会收到&QUOT;“结果”有没有属性'的findAll'&QUOT;使用BeautifulSoup在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!