Python urllib3有太多的重定向

时间:2016-02-12 11:45:32

标签: python python-3.x urllib3

我现在学习Python,我想创建简单的工具来打开几个站点。我有下一个代码:

#!/usr/bin/python
import urllib3, ssl, certifi
from urllib3 import Retry, Timeout

def openurl(url, method = "get"):
    retries = Retry(connect=500, read=2, redirect=500)
    http = urllib3.PoolManager(
        cert_reqs = 'CERT_REQUIRED',
        ca_certs = certifi.where(),
        retries = retries
    )
    con = urllib3.connection_from_url(url)
    r = con.request(method, '/trades');

openurl("http://www.steamgifts.com")

但是在此网站上,脚本会返回Caused by ResponseError('too many redirects',)

我尝试通过Retry(connect=500, read=2, redirect=500)解决此问题,但我没有看到更改。

1 个答案:

答案 0 :(得分:2)

该网站阻止了一些用户代理。您可以通过为请求设置自己的http标头来假装成为真正的webbrowser而不是偷偷摸摸的黑客。我对urllib3并不熟悉,但requests使用它非常简单。

>>> requests.get('http://www.steamgifts.com/trades')
<Response [403]>

>>> requests.get('http://www.steamgifts.com/trades', 
    headers={'User-Agent': 'internet explorer or something'})
<Response [200]>
相关问题