从简单的 get 返回 403 Forbidden 但在浏览器中加载正常

2023-12-15

我试图从页面获取一些数据,但它返回错误 [403 禁忌].

我以为是用户代理,但我尝试了几个用户代理,它仍然返回错误。

我也尝试使用图书馆假用户代理但我没有成功。

with requests.Session() as c:
        url = '...'
        #headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36'}
        ua = UserAgent()
        header = {'User-Agent':str(ua.chrome)}
        page = c.get(url, headers=header)
        print page.content

当我手动访问该页面时,一切正常。

我正在使用 python 2.7.14 和请求库, 任何想法?


该网站可能会使用请求中的任何内容来触发拒绝。

So, copy all headers from the request that your browser makes. Then delete them one by one1 to find out which are essential.

As per Python 请求。 403 禁忌,要将自定义标头添加到请求中,请执行以下操作:

result = requests.get(url, headers={'header':'value', <etc>})

1A faster way would be to delete half of them each time instead but that's more complicated since there are probably multiple essential headers

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从简单的 get 返回 403 Forbidden 但在浏览器中加载正常 的相关文章

随机推荐