【问题标题】:AttributeError: 'bytes' object has no attribute 'find_all'AttributeError:“字节”对象没有属性“find_all”
【发布时间】:2020-03-11 02:24:27
【问题描述】:

我正在尝试通过网络抓取板球得分 webiste 以获得记分卡。 但我收到此属性错误:

回溯(最近一次通话最后一次):
**文件“J:/Python Programs/Web Scraper/ESPN Cric Info.py”,第 6 行,在

soup = BeautifulSoup.find(page.content, 'html.parser')

文件“J:\Python Programs\Web Scraper\venv\lib\site-packages\bs4\element.py”,第 1282 行,在查找中 l = self.find_all(name, attrs, recursive, text, 1, ****kwargs**) ** AttributeError: 'bytes' 对象没有属性 'find_all'****

我的代码是:

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.espncricinfo.com/series/19430/scorecard/1187016/india-vs-bangladesh-1st-test-icc-world-test-championship-2019-2021')
soup = BeautifulSoup.find(page.content, 'html.parser')
scorecard = soup.find(id='gp-inning-01')

print(scorecard)

如果你解决了这将是一个很大的帮助。

【问题讨论】:

  • 请显示整个堆栈跟踪。

标签: python web-scraping beautifulsoup html-parsing


【解决方案1】:
import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.espncricinfo.com/series/19430/scorecard/1187016/india-vs-bangladesh-1st-test-icc-world-test-championship-2019-2021')
soup = BeautifulSoup(page.content, 'html.parser')
scorecard = soup.find_all('gp-inning-01')

print(scorecard)

试试这个

【讨论】:

  • @Captain.026 如果答案对您有帮助,请为答案投票。并将最有帮助的答案标记为问题的正确答案,以便其他寻找类似问题的人快速获得帮助。
【解决方案2】:

你犯了两个小错误,但你的代码几乎是正确的:

  • 您需要在BeautifulSoup 之后删除.find,因为此行仅用于创建汤而不是搜索任何内容
  • 在检索请求时,您需要使用 .text 而不是 .content,因为 BeautifulSoup 使用的是字符串而不是字节。

这是最终代码:

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.espncricinfo.com/series/19430/scorecard/1187016/india-vs-bangladesh-1st-test-icc-world-test-championship-2019-2021')
soup = BeautifulSoup(page.text, 'html.parser')
scorecard = soup.find(id='gp-inning-01')

print(scorecard)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2016-08-16
    相关资源
    最近更新 更多