首页 > 解决方案 > 响应 [403] 添加用户代理和其他标头后 Python 请求出错,Cloudfare 阻止访问

问题描述

尽管添加了从 chrome 开发人员工具获得的标头,但我还是多次收到 <Response [403]>。有更多经验的人能否告诉我是否可以使用 Python 请求访问以下 url?如果没有,是否有建议的替代方法

url='https://www.phosphosite.org/proteinAction.action?id=5848&showAllSites=true'

from bs4 import BeautifulSoup
import requests
url='https://www.phosphosite.org/proteinAction.action?id=5848&showAllSites=true'
headers={'user-agent': 'Mozilla/5.0....'}
result = requests.get(url, headers=headers)
print(result.request.headers)
print(result)

标签: pythonpython-requests

解决方案


因为header key是小写的 怎么解决? 更改您的标题键:

'user-agent'

至:

'User-Agent'

然后你得到<Response [200]>结果。


推荐阅读