使用Python的请求lib抛出ProxyError

时间:2016-08-18 13:51:11

标签: python python-requests

我还是python的新手,无法弄清楚如何处理这个错误以及如何处理它以避免它。

当我使用requests.get('http://www.baidu.com')

    import requests
    header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
    h=requests.get('http://www.baidu.com',headers=header)
    print h.text

抛出ProxyError

Traceback (most recent call last):
  File "D:/freedomcoder/Code/Python/rexx/rexx.py", line 8, in <module>
    h = requests.get('http://github.com/kennethreitz/requests/issues/3050',headers=header)
  File "C:\Python27\lib\site-packages\requests\api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 596, in send
   r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 485, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='107.160.9.10', port=80): Max retries exceeded with url: http://www.baidu.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x02EE4170>: Failed to establish a new connection: [Errno 10061] ',)))

但是当我使用requests.get('https://www.baidu.com')时,它会返回正确的页面。我不知道为什么会这样。

2 个答案:

答案 0 :(得分:2)

我处理这个问题。

只需添加代理= {&#39; http&#39;:&#39;&#39;&#39; https&#39;:&#39;&#39;}

例如:

h=requests.get('http://www.baidu.com', proxies={'http':'','https':''})

答案 1 :(得分:-1)

当我测试你的代码时,它似乎工作。

问题可能是其他人声明的网络连接错误。

通过cli验证问题的一种方法(连接应该打开,就像在这个例子中,soconnection成功打开...)

$ telnet baidu.com 80 
Trying 220.181.57.217...
Connected to baidu.com.
Escape character is '^]'.