【问题标题】:How to remove tags with find_all in Beautifulsoup?如何在 Beautifulsoup 中使用 find_all 删除标签?
【发布时间】:2020-01-24 20:58:09
【问题描述】:

我已经能够从 find 中删除文本,但是在使用 find_all 时会出现错误。


equipmentType = category.find_all("div", {"class":"ExResult-details ExResult-equipmentType"}).text


print(equipmentType)`


`Traceback (most recent call last):
  File "scrape.py", line 17, in <module>
    equipmentType = category.find_all("div", {"class":"ExResult-details ExResult-equipmentType"}).text
  File "/home/bert/.local/lib/python2.7/site-packages/bs4/element.py", line 1578, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
`

【问题讨论】:

标签: python beautifulsoup screen-scraping


【解决方案1】:

答案在 Traceback 的最后一行:

ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item.

find_all() 返回一个集合。您要做的是遍历集合,并获取每个元素的文本:

equipment_list = category.find_all("div", {"class":"ExResult-details ExResult-equipmentType"})

for equipmentType in equipment_list:
    print(equipmentType.text)

【讨论】:

    【解决方案2】:

    来自错误信息:You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

    也就是说find_all 返回一个ResultSet 并且ResultSet 没有属性text。也许试试:

    equipment_type = [item.text for item in category.find_all("div", {"class":"ExResult-details ExResult-equipmentType"})]
    

    【讨论】:

      【解决方案3】:

      您应该像这样遍历结果集:

      mydivs = [item.text.strip() for item in page_soup.find_all("div", {"id":"pil_name"})]
      

      这只是我的代码中的一个示例,因为我遇到了同样的问题

      【讨论】:

        猜你喜欢
        • 2022-11-23
        • 1970-01-01
        • 2020-10-27
        • 1970-01-01
        • 2017-03-13
        • 2020-02-26
        • 2017-03-31
        • 1970-01-01
        • 2023-01-04
        相关资源
        最近更新 更多