Ale*_*lex 8 python urllib2 beautifulsoup
所以我正在慢慢地学习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'
Run Code Online (Sandbox Code Playgroud)
提前致谢.
ber*_*nie 19
哇.Triptych为相关问题提供了很好的答案.
我们可以从BeautifulSoup的源代码中看到那个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
Run Code Online (Sandbox Code Playgroud)
Leonard Richardson的注意事项:我绝不打算将其称为BS ;-)打算贬低你的工作质量
| 归档时间: |
|
| 查看次数: |
15080 次 |
| 最近记录: |