【问题标题】:ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single elementResultSet 对象没有属性“find_all”。您可能将元素列表视为单个元素
【发布时间】:2021-02-14 06:55:21
【问题描述】:

我有一个问题,我想抓取所有数据值,但脚本只先抓取,所以我将 find 更改为 find_all 并且我得到错误。 希望有人可以帮助我。

脚本:

import bs4
import lxml
import requests
from bs4 import BeautifulSoup

link=('www.site.com')

def Data():
   a=soup.find_all("div",{'class':'selector'})     
   b=a.find_all("li").attrs.get("data-value", None)  
   return b

这是网站代码

<div class="selector">

【问题讨论】:

  • 答案在错误信息中。

标签: python beautifulsoup python-requests lxml


【解决方案1】:

问题正是错误消息所说的您将列表视为单个元素

import bs4
import lxml
import requests
from bs4 import BeautifulSoup

link=('www.site.com')

def Data():
    output = []

    #find_all returns a list so use find in a

    a=soup.find("div",{'class':'selector'})     
    b=a.find_all("li")
    for element in b:
        link = element.attrs.get("data-value", None)  
        output.append(link)

    return output

【讨论】:

  • 它仍然只废弃第一个数据值
  • @DemonsAreHere4000 我已经编辑了答案以获得你想要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2016-12-19
  • 2013-03-30
  • 2021-10-17
  • 2017-04-25
相关资源
最近更新 更多