所以我正在慢慢学习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'
提前致谢。
哇。三联画提供了great answer https://stackoverflow.com/questions/989872/how-do-i-draw-out-specific-data-from-an-opened-url-in-python-using-urllib2/989920#989920相关问题。
我们可以看到,来自BeautifulSoup的源代码 http://code.google.com/p/google-blog-converters-appengine/source/browse/trunk/lib/BeautifulSoup.py, that ResultSet
子类list http://docs.python.org/tutorial/datastructures.html#more-on-lists.
在你的例子中,get_rows
是 BS 的一个实例ResultSet
班级,
自从 BS 以来ResultSet
子类list
,这意味着get_rows 是一个列表.
get_rows
,作为一个实例ResultSet
, does not have a findAll
实施的方法;因此你的错误。
Triptych 所做的不同之处在于iterate在那张清单上。
Triptych 的方法之所以有效,是因为get_rows
list 是 BS 的 Tag 类的实例;其中有一个findAll
method.
因此,要修复您的代码,您可以替换您的最后三行create
方法如下:
for row in get_rows:
text = ''.join(row.findAll(text=True))
data = text.strip()
print data
伦纳德·理查森请注意:我绝不打算通过将其称为“BS”来贬低您的工作质量;-)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)