简体   繁体   English

python - requests.get(连接中止。',OSError(“(60,'ETIMEDOUT')

[英]python - requests.get (Connection aborted.', OSError("(60, 'ETIMEDOUT')

I'm new to web scraper, tried to access a website but got too many errors and was told (Connection aborted.', OSError("(60, 'ETIMEDOUT').我是 web 抓取工具的新手,尝试访问网站但出现太多错误并被告知(连接中止。',OSError(“(60,'ETIMEDOUT')。

from bs4 import BeautifulSoup
import requests
urla="https://www.loopnet.com/search/office-space/san-diego-ca/for-lease/?sk=02fa1ad85634ef43bfd21f24bbe3a14a"
source = requests.get(urla).text
print(source)

OSError Traceback (most recent call last) ~/opt/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 671 headers=headers, --> 672 chunked=chunked, 1343 try: -> 1344 response.begin() 1345 except ConnectionError: OSError Traceback(最近一次调用最后一次)~/opt/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 671 headers=headers, --> 672 chunked=chunked, 1343 try: -> 1344 response.begin() 1345 除了 ConnectionError:

~/opt/anaconda3/lib/python3.7/http/client.py in begin(self) 305 while True: --> 306 version, status, reason = self._read_status() 307 if status:= CONTINUE: ~/opt/anaconda3/lib/python3.7/http/client.py in begin(self) 305 while True: --> 306 version, status, reason = self._read_status() 307 if status:= CONTINUE:

~/opt/anaconda3/lib/python3.7/http/client.py in _read_status(self) 266 def _read_status(self): --> 267 line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") 268 if len(line) > _MAXLINE: ~/opt/anaconda3/lib/python3.7/http/client.py in _read_status(self) 266 def _read_status(self): --> 267 line = str(self.fp.readline(_MAXLINE + 1), "iso -8859-1") 268 如果 len(line) > _MAXLINE:

~/opt/anaconda3/lib/python3.7/socket.py in readinto(self, b) 588 try: --> 589 return self._sock.recv_into(b) 590 except timeout: ~/opt/anaconda3/lib/python3.7/socket.py in readinto(self, b) 588 try: --> 589 return self._sock.recv_into(b) 590 超时除外:

~/opt/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py in recv_into(self, *args, **kwargs) 317 else: --> 318 raise SocketError(str(e)) 319 except OpenSSL.SSL.ZeroReturnError: ~/opt/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py 在 recv_into(self, *args, **kwargs) 317 else: --> 318 raise SocketError(str(e)) 319 除了 OpenSSL.SSL.ZeroReturnError:

OSError: (60, 'ETIMEDOUT')操作系统错误:(60,'ETIMEDOUT')

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

ProtocolError Traceback (most recent call last) ~/opt/anaconda3/lib/python3.7/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies) 448 retries=self.max_retries, --> 449 timeout=timeout 450 ) ProtocolError Traceback(最近一次调用最后)~/opt/anaconda3/lib/python3.7/site-packages/requests/adapters.py 在发送(自我,请求,stream,超时,验证,证书,代理)448 次重试=自我.max_retries, --> 449 timeout=timeout 450 )

~/opt/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 719 retries = retries.increment( --> 720 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2] 721 ) ~/opt/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, 重试, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 719 次重试 = retries.increment( --> 720 方法,url,error=e,_pool=self,_stacktrace=sys.exc_info()[2] 721)

ConnectionError: ('Connection aborted.', OSError("(60, 'ETIMEDOUT')")) (Something like this) ConnectionError: ('Connection aborted.', OSError("(60, 'ETIMEDOUT')")) (类似这样)

The reason you are getting this error is because the server is not responding or is taking too long to respond.您收到此错误的原因是服务器没有响应或响应时间过长。 you should always encapsulate the requests in a try block to avoid program crash, This way you can catch any error, or specific errors by using the requests.exceptions module您应该始终将请求封装在 try 块中以避免程序崩溃,这样您就可以使用 requests.exceptions 模块捕获任何错误或特定错误

import requests
url="https://www.loopnet.com/search/office-space/san-diego-ca/for-lease/?sk=02fa1ad85634ef43bfd21f24bbe3a14a"

Catch any error捕捉任何错误

try:
    source = requests.get(url).text
except:
    print 'ERROR'

Catch ConnectionError捕获连接错误

from requests.exceptions import ConnectionError

try:
    source = requests.get(url).text
except ConnectionError:                  # <-- this is your case scenario
    print 'NOT RESPONDING'

Catch TimeoutError捕捉超时错误

from requests.exceptions import ReadTimeout

try:
    source = requests.get(url, timeout=2).text  # <-- you should always use timeout to avoid requests hanging or taking too long to respond
except ReadTimeout:
    print 'TIMEOUT'

You can view all the requests exceptions like this您可以像这样查看所有请求异常

for exception in dir(requests.exceptions):
    print exception

Read requests exceptions docs here: https://requests.readthedocs.io/en/latest/user/quickstart/#errors-and-exceptions在此处阅读请求异常文档: https://requests.readthedocs.io/en/latest/user/quickstart/#errors-and-exceptions

暂无
暂无

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

相关问题 Web 抓取 - 请求 ConnectionError: ('Connection aborted.', OSError(“(60, 'ETIMEDOUT')”,)) - Web Scraping - Requests ConnectionError: ('Connection aborted.', OSError(“(60, 'ETIMEDOUT')”,)) 使用 python 请求获取 (&#39;Connection aborted.&#39;, OSError(0, &#39;Error&#39;) 错误 - Getting ('Connection aborted.', OSError(0, 'Error') errors with python requests Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,)) - Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,)) Python请求&#39;连接中止。&#39; 如果它将以cronjob开始 - Python requests 'Connection aborted.' if it will started with a cronjob 如何在 Python 中跳过“Connection aborted.”、OSError(0, 'Error')? - How can I skip 'Connection aborted.', OSError(0, 'Error') in Python? Python 网络抓取错误('连接中止',OSError(“(10060,'WSAETIMEDOUT')”)) - Python Webscraping Error ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) - Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) 请求超时错误 (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