簡體   English   中英

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

[英]python - requests.get (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(最近一次調用最后一次)~/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 _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 超時除外:

~/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:

操作系統錯誤:(60,'ETIMEDOUT')

在處理上述異常的過程中,又出現了一個異常:

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, 重試, 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')")) (類似這樣)

您收到此錯誤的原因是服務器沒有響應或響應時間過長。 您應該始終將請求封裝在 try 塊中以避免程序崩潰,這樣您就可以使用 requests.exceptions 模塊捕獲任何錯誤或特定錯誤

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

捕捉任何錯誤

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

捕獲連接錯誤

from requests.exceptions import ConnectionError

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

捕捉超時錯誤

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'

您可以像這樣查看所有請求異常

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

在此處閱讀請求異常文檔: https://requests.readthedocs.io/en/latest/user/quickstart/#errors-and-exceptions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM