![](/img/trans.png)
[英]Beautiful Soup 'ResultSet' object has no attribute 'text'
[英]Beautiful soup - 'ResultSet' object has no attribute 'text'
我试图从锚标记中仅提取文本。 我知道find_all
返回一个ResultSet对象,并且需要对其进行迭代,但是,我似乎无法使其正常工作。 我的代码如下:
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
显然,我在做错事,但不确定是什么?
问题是第一个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,
“名称”是列表,其中包含所有“ a”标签元素。
您不能在列表上使用.text属性。
据我了解,下面的代码应该工作
for the_name in Name:
print(the_name.text)
您的方法是正确的。 但是您在上述循环中犯的错误是,对于列表中的每个元素,您都调用了element(“ the_name”)而不是列表(“ Name”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.