【问题标题】:When using 'requests.get()', the html is less than the actual html使用“requests.get()”时,html 小于实际 html
【发布时间】:2021-01-14 13:24:26
【问题描述】:

第一张图是python引入的网站的html,第二张图是网站上按F12查看的实际html。我不知道为什么这两个结果不同。其他网站正常显示html,不知道为什么只有那个网站不能正常显示。

代码如下:

import requests
from bs4 import BeautifulSoup


result = requests.get('https://www.overbuff.com/heroes')

soup = BeautifulSoup(result.text, "html.parser")

print(soup)

【问题讨论】:

  • 因为回复是" the page you are trying to reach is temporarily unavailable. please try again later "

标签: python html beautifulsoup python-requests python-requests-html


【解决方案1】:

您可能被该页面屏蔽了,应该尝试使用 headers 之类的:

headers = {"user-agent": "Mozilla/5.0"}    
r = requests.get(url, headers=headers)

示例

import requests
from bs4 import BeautifulSoup
url = "https://www.overbuff.com/heroes"
headers = {"user-agent": "Mozilla/5.0"}

r = requests.get(url, headers=headers)

soup = BeautifulSoup(r.text, "html.parser")

print(soup)

【讨论】:

  • 只是更改用户代理对我不起作用,但在从浏览器请求中复制所有标头后,我得到了它。看起来该网站正在使用多个标头字段来检测机器人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2017-01-15
  • 2012-04-22
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 2021-01-25
相关资源
最近更新 更多