为什么解析的标签名称不同?

2024-04-29 05:33:46 发布

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

from bs4 import BeautifulSoup
import requests

web_url = r'https://www.mlb.com/scores/2019-05-12'
get_web = requests.get(web_url).text
soup = BeautifulSoup(get_web,"html.parser")
score = soup.find_all('div',class_='container')
print(score)

我想找到this

但结果是this


Tags: fromhttpsimportcomweburlgetwww
1条回答
网友
1楼 · 发布于 2024-04-29 05:33:46

向API发送标题,告诉它“嘿,我是桌面浏览器”,以便从服务器端获得相同的HTML:

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
request = requests.get(url, headers={'User-Agent': user_agent})

有用的链接:

  1. How to use Python requests to fake a browser visit?
  2. Sending "User-agent" using Requests library in Python

相关问题 更多 >