简体   繁体   English

如何在 Python 中跳过“Connection aborted.”、OSError(0, 'Error')?

[英]How can I skip 'Connection aborted.', OSError(0, 'Error') in Python?

1 try:
2    req = requests.get(my_url, timeout=20)
3 except requests.exceptions.ConnectionError:
4    time.sleep(10)
5    req = requests.get(my_url, timeout=30)
6 except:
7    time.sleep(10)
8    req = requests.get(my_url, timeout=20)

When I run this code, there is often on error on line 2.当我运行这段代码时,第 2 行经常出现错误。

Traceback (most recent call last):
  File "E:/python/kr.py", line 2, in <module>
    req = requests.get(my_url, timeout=20)
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 665, in send
    history = [resp for resp in gen]
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 665, in <listcomp>
    history = [resp for resp in gen]
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 237, in resolve_redirects
    resp = self.send(
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\icarus-DT\PycharmProjects\untitled\venv\lib\site-packages\requests\adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error'))

Is there any way that I can skip this error and move to time.sleep line?有什么办法可以跳过这个错误并转到 time.sleep 行吗? Thank you in advance!先感谢您!

I don't believe python allows except block after another except block, you can try a nested approach as below我不相信 python 允许 except 块接一个 except 块,你可以尝试下面的嵌套方法

try:
    req = requests.get(my_url, timeout=20)
except requests.exceptions.ConnectionError:
    try:
       time.sleep(10)
       req = requests.get(my_url, timeout=20)
    except Exception as e:
       print(e)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 网络抓取错误('连接中止',OSError(“(10060,'WSAETIMEDOUT')”)) - Python Webscraping Error ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) 使用 python 请求获取 (&#39;Connection aborted.&#39;, OSError(0, &#39;Error&#39;) 错误 - Getting ('Connection aborted.', OSError(0, 'Error') errors with python requests python - requests.get(连接中止。',OSError(“(60,'ETIMEDOUT') - python - requests.get (Connection aborted.', OSError("(60, 'ETIMEDOUT') Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) - Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) 如何修复“连接中止”。 使用 BeautifulSoup 在 Python 中出错 - How to fix 'Connection aborted.' error in Python with BeautifulSoup Python-异常使“连接异常终止”-如何跳过它并继续 - Python - Exception gives 'Connection aborted' - how can I skip it and continue Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,)) - Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,)) 请求超时错误 (requests.exceptions.ConnectionError: (&#39;Connection aborted.&#39;, OSError(&quot;(10060, &#39;WSAETIMEDOUT&#39;)&quot;))) - Requests Timeout Error (requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(10060, 'WSAETIMEDOUT')"))) Python-请求lib-错误(“连接已中止。”,BadStatusLine(“&#39;&#39;”,)) - Python - requests lib - error ('Connection aborted.', BadStatusLine(“''”,)) Python 请求获取 (&#39;Connection aborted.&#39;, BadStatusLine(&quot;&#39;&#39;&quot;,)) 错误 - Python Requests getting ('Connection aborted.', BadStatusLine("''",)) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM