简体   繁体   English

漂亮的汤-'ResultSet'对象没有属性'text'

[英]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.

相关问题 美丽的汤'ResultSet'对象没有属性'text' - Beautiful Soup 'ResultSet' object has no attribute 'text' Beautiful Soup WebScraping错误-ResultSet对象没有属性'%s' - Beautiful Soup WebScraping Error - ResultSet object has no attribute '%s' 美丽的汤:“ ResultSet”对象没有属性“ find_all”吗? - Beautiful Soup: 'ResultSet' object has no attribute 'find_all'? Beautiful Soup 中 span 标签上的 find_all 产生 AttributeError:ResultSet 对象没有属性“get_text” - find_all on span tag in Beautiful Soup yields AttributeError: ResultSet object has no attribute 'get_text' 美丽的汤有错误:'NoneType' 对象没有属性 'text' - beautiful soup with error : 'NoneType' object has no attribute 'text' 美丽的汤:AttributeError: 'NoneType' 对象没有属性 'text' - Beautiful soup: AttributeError: 'NoneType' object has no attribute 'text' Python Beautiful Soup“ NavigableString”对象没有属性“ get_text” - Python Beautiful Soup 'NavigableString' object has no attribute 'get_text' 美丽的Soup4'NoneType'对象没有属性'text'错误 - Beautiful Soup4 'NoneType' object has no attribute 'text' Error 用 Beautiful Soup 解析跨度:'NoneType' object 没有属性 'text' - Parse span with Beautiful Soup : 'NoneType' object has no attribute 'text' 'ResultSet'对象在函数中的Beautiful Soup中没有属性'findAll'错误消息 - 'ResultSet' object has no attribute 'findAll' error message in Beautiful Soup within a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM