我正在尝试使用Python(2.7) requests模块请求此页面的https://health.usnews.com/best-hospitals/rankings/cancer。但它给出了403响应(它在我的本地机器上工作得很好,但在服务器上不工作)。
绕过请求中的标头和cookies请求页面。但是得到了403的响应。此外,还按照Python requests - 403 forbidden - despite setting User-Agent
headers中的建议尝试了Session
对象
>>> requests.get('https://health.usnews.com/best-hospitals/rankings/cancer')
<Response [403]>
>>> requests.get('https://health.usnews.com/best-hospitals/rankings/cancer', headers=h)
<Response [403]>
我们如何才能从该页面获得正确的响应?
提前谢谢你!
发布于 2019-07-10 14:52:02
发起请求时需要使用headers中的User-Agent
:
import requests
url = 'https://health.usnews.com/best-hospitals/rankings/cancer'
headers = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0'}
txt = requests.get(url, headers=headers).text
print(txt)
打印:
<!doctype html>
<html class="no-js" lang="">
<head>
... and so on.
https://stackoverflow.com/questions/56964873
复制相似问题