Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我会得到';结果集';没有属性';findAll'&引用;在Python中使用BeautifulSoup?_Python_Urllib2_Beautifulsoup - Fatal编程技术网

为什么我会得到';结果集';没有属性';findAll'&引用;在Python中使用BeautifulSoup?

为什么我会得到';结果集';没有属性';findAll'&引用;在Python中使用BeautifulSoup?,python,urllib2,beautifulsoup,Python,Urllib2,Beautifulsoup,因此,我正在慢慢学习Python,并试图制作一个简单的函数,从在线游戏的高分页面中提取数据。这是其他人的代码,我将其重写为一个函数(这可能是问题所在),但我遇到了这个错误。代码如下: >>> from urllib2 import urlopen >>> from BeautifulSoup import BeautifulSoup >>> def create(el): source = urlopen(el).read()

因此,我正在慢慢学习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'
>>从urllib2导入urlopen
>>>从BeautifulSoup导入BeautifulSoup
>>>def创建(el):
source=urlopen(el).read()
汤=美汤(来源)
get_table=soup.find('table',{'id':'mini_player'})
get_rows=get_table.findAll('tr')
text=''.join(get_rows.findAll(text=True))
data=text.strip()
返回数据
>>>创建('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
创建('http://hiscore.runescape.com/hiscorepersonal.ws?user1=bigdrizzle13')
文件“”,第6行,在“创建”中
text=''.join(get_rows.findAll(text=True))
AttributeError:'ResultSet'对象没有属性'findAll'
提前谢谢。

哇。三联画为一个相关问题提供了答案

我们可以看到,
ResultSet
子类

在您的示例中,
get_rows
是BS的
ResultSet
类的一个实例,
由于BS的
ResultSet
子类
list
,这意味着get\u行是一个列表

get_rows
,作为
ResultSet
的一个实例,not是否实现了
findAll
方法;这就是你的错误
Triptych所做的不同之处在于对该列表进行迭代
Triptych的方法之所以有效,是因为
get_行
列表中的项目是BS的标记类的实例;它有一个
findAll
方法

因此,要修复代码,可以将
create
方法的最后三行替换为如下内容:

for row in get_rows:
    text = ''.join(row.findAll(text=True))
    data = text.strip()
    print data

Leonard Richardson注意:我决不想通过称你的作品为BS来贬低它的质量;-)

值得一提的是:将变量命名为“第一”、“第二”等是一种糟糕的风格。你真的应该更具描述性——具体的名称当然由你决定,但我可能会使用“urlcontent”、“parser”、“mp_tables”等等。这是我学习Python的第三天。我需要这样做来保持头脑清醒。随着时间的推移,这会变得更好…我更改了变量名。希望这样更好。
text=True
正是我想要的!