[英]Beautiful soup - 'ResultSet' object has no attribute 'text'
I am trying to extract just the text from an anchor tag. 我试图从锚标记中仅提取文本。 I understand that
find_all
returns a ResultSet object and that I need to iterate over it, however, I can't seem to get it work. 我知道
find_all
返回一个ResultSet对象,并且需要对其进行迭代,但是,我似乎无法使其正常工作。 My code below: 我的代码如下:
for all in soup.find("div", {"id": "info-area"}):
Name = all.find_all("a")
#print(Name) # Returns everything
#print(Name.text) # throws error
for the_name in Name:
print(Name.text) # throws error
Obviously, I'm doing something wrong but not quite sure what? 显然,我在做错事,但不确定是什么?
The problem is the first for loop, change your code to: 问题是第一个for循环,将代码更改为:
all_div = soup.find("div", {"id": "info-area"}) #find div with id = info-area
Name = all_div.find_all("a") # under all_div find all a
for the_name in Name: #loop through each a
print(the_name.text) #print each a text
@Maverick, @Maverick,
"Name" is the list, which has all 'a' tag elements. “名称”是列表,其中包含所有“ a”标签元素。
You can not use .text attribute on a list. 您不能在列表上使用.text属性。
To my understanding, the below code should work 据我了解,下面的代码应该工作
for the_name in Name:
print(the_name.text)
Your approach is correct. 您的方法是正确的。 But the mistake you did in the above loop is, for each element in the list you have call the element("the_name") not the list ("Name")
但是您在上述循环中犯的错误是,对于列表中的每个元素,您都调用了element(“ the_name”)而不是列表(“ Name”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.