Python Beautiful Soup 'ResultSet' 对象没有属性 'text'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36091242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Beautiful Soup 'ResultSet' object has no attribute 'text'
提问by Frank
from bs4 import BeautifulSoup
import urllib.request
import win_unicode_console
win_unicode_console.enable()
link = ('https://pietroalbini.io/')
req = urllib.request.Request(link, headers={'User-Agent': 'Mozilla/5.0'})
url = urllib.request.urlopen(req).read()
soup = BeautifulSoup(url, "html.parser")
body = soup.find_all('div', {"class":"wrapper"})
print(body.text)
Hi, I have a problem with Beautiful Soup, if I run this code without ".text" at the end it show me a list of div but if I add ".text" at the end come the error
嗨,我有一个关于 Beautiful Soup 的问题,如果我在最后没有“.text”的情况下运行这段代码,它会显示一个 div 列表,但如果我在最后添加“.text”就会出现错误
Traceback (most recent call last): File "script.py", line 15, in print(body.text) AttributeError: 'ResultSet' object has no attribute 'text'
回溯(最近一次调用):文件“script.py”,第 15 行,在打印(body.text)中 AttributeError: 'ResultSet' object has no attribute 'text'
回答by Ahmed Dhanani
find_all
returns a ResultSet object which you can iterate over using a for
loop. What you can do is:
find_all
返回一个 ResultSet 对象,您可以使用for
循环对其进行迭代。你可以做的是:
for wrapper in body.find_all('div', {"class":"wrapper"}):
print wrapper.text
回答by Mikhail Gerasimov
If you'll type:
如果你输入:
print(type(body))
you'll see body
is <class 'bs4.element.ResultSet'>
It means allthe elements that match the class. You can either iterate over them:
你会看到body
is<class 'bs4.element.ResultSet'>
表示与类匹配的所有元素。您可以遍历它们:
for div in body:
print(div.text)
Or if you know you only have div, you can use find
instead:
或者,如果您知道您只有 div,则可以使用find
:
div = soup.find('div', {"class":"wrapper"})
div.text
回答by Pythonista
Probably should have posted as answer.. so as stated in the comments almost verbatim
可能应该作为答案发布......所以正如评论中几乎逐字所述
Your code should be the following:
您的代码应如下所示:
for div in body:
print div.text
#python3
#print(div.text)
Or some naming schema to your preference thereof.
或者一些你喜欢的命名模式。
The find_all
method returns a generated list ( loosely using the term list here ) of items that beautifulsoup has found matching your criteria after parsing the source webpages html either recursively or non-recursively depending upon how you search.
该find_all
方法返回一个生成的列表(在这里松散地使用术语列表),在根据您的搜索方式递归或非递归地解析源网页 html 后,beautifulsoup 发现匹配您的条件的项目。
As the error says the resulting set of objects has no attribute text, since it isn't an element but rather a collection of them. However, the items inside the resulting set ( should any be found ) do.
正如错误所说,结果对象集没有属性文本,因为它不是元素而是它们的集合。但是,结果集内的项目(应该有任何发现)可以。
You can view the documentation here
您可以在此处查看文档