AttributeError:“bytes”对象没有“find\u all”属性

2024-04-28 16:17:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过网络刮蟋蟀得分webiste得到一个记分卡。 但是我得到了这个属性错误:

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

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

文件“J:\Python Programs\Web Scraper\venv\lib\site packages\bs4\元素.py,第1282行,查找 l=自我发现(名称,属性,递归,文本,1,****kwargs**) ** AttributeError:'bytes'对象没有属性'find\u 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)

如果你能解决这个问题,那将是一个很大的帮助。你知道吗


Tags: 文件pywebparser属性htmlpagecontent
2条回答

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

  • 您需要删除.find之后的BeautifulSoup,因为这一行只用于创建汤,而不用于搜索任何内容
  • 检索请求时需要使用.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)
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)

试试这个

相关问题 更多 >