[英]Beautiful Soup 'ResultSet' object has no attribute 'text'
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 嗨,我有一个美丽的汤的问题,如果我运行此代码没有“.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'对象没有属性'text'
find_all
returns a ResultSet object which you can iterate over using a for
loop. find_all
返回一个ResultSet对象,您可以使用for
循环迭代它。 What you can do is: 你能做的是:
for wrapper in body.find_all('div', {"class":"wrapper"}):
print wrapper.text
If you'll type: 如果你输入:
print(type(body))
you'll see body
is <class 'bs4.element.ResultSet'>
It means all the elements that match the class. 你会看到
body
是<class 'bs4.element.ResultSet'>
它表示所有与该类匹配的元素。 You can either iterate over them: 你可以迭代它们:
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
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
方法返回一个生成的列表(松散地使用这里的术语列表),这些项目是beautifulsoup找到的符合条件的项目,在根据您搜索的方式递归或非递归地解析源网页html之后。
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.
但是,结果集内的项目(应该找到)都可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.