我正在尝试异常处理这些代理错误,我正在尝试关注但没有成功:
from requests.exceptions import ProxyError, SSLError, ConnectionError
def adding(self):
headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36'
}
url = 'url'
r = self.session.get(url, headers=headers)
def run(self):
while True:
try:
self.adding()
except OSError as e:
print 'OSError'
except ProxyError as g:
print 'ProxyError'
然而,当我运行python脚本时,我仍然在日志中出错:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "xx.py", line 273, in run
self.adding()
File "xx.py", line 107, in adding
r = self.session.get(url, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/adapters.py", line 502, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='url', port=443): Max retries exceeded with url: url (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 Bad Request',)))
答案 0 :(得分:0)
您的例外下方的print
语句不会缩进为其各自except
语句的一部分,而您的try
似乎没有正确缩进。我不确定这是否能解决你的问题,但它似乎是无意的。