python - 为什么我得到 "' ResultSet' has no attribute 'findAll'“在 Python 中使用 BeautifulSoup?

标签 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'

提前致谢。

最佳答案

哇。 Triptych 提供了 great answer一个相关的问题。

我们可以看到,from BeautifulSoup's source code , ResultSet 子类 list .

在您的示例中,get_rows 是 BS 的 ResultSet 类的一个实例,
由于 BS 的 ResultSetlist 的子类,这意味着 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/

相关文章:

python + 运算符重载问题

python - 需要转置 Pandas 数据框

python - 在启用 cookie 的站点上使用 urlretrieve 的多线程网络抓取工具

python - 使用 python 脚本从 informer.com 抓取和下载文件

python - 有没有一种方法可以测试两个形状是否可以在numpy中广播?

python - 如何在 Python 中对存储在字典中的 IP 地址进行排序?

python - 带有 cookie 的 urllib2

python - 使用 BeautifulSoup 抓取网站以下载其上的所有文档会抛出 IOError

python - 无法从一些不同深度的链接中解析产品名称