簡體   English   中英

Web 抓取 - 請求 ConnectionError: ('Connection aborted.', OSError(“(60, 'ETIMEDOUT')”,))

[英]Web Scraping - Requests ConnectionError: ('Connection aborted.', OSError(“(60, 'ETIMEDOUT')”,))

我正在嘗試訪問網頁。 我嘗試“UserAgent”添加標題,但是出現超時錯誤:我的新代碼:

from fake_useragent import UserAgent
import requests

url = "https://www.bestbuy.com/site/lg-65-class-oled-b9-series-2160p-smart-4k-uhd-tv-with-hdr/6360611.p?skuId=6360611"
ua = UserAgent()
print(ua.chrome)
header = {'User-Agent':str(ua.chrome)}
print(header)
url_get = requests.get(url, headers=header)
print(url_get)    

--> 285 引發 SocketError(str(e)) 286 除了 OpenSSL.SSL.ZeroReturnError 為 e:

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

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

ProtocolError Traceback(最近一次調用最后一次)/anaconda3/lib/python3.6/site-packages/requests/adapters.py 在發送(自我,請求,stream,超時,驗證,證書,代理)439 次重試=self.max_retries, --> 440 超時=超時 441 )

--> 285 引發 SocketError(str(e)) 286 除了 OpenSSL.SSL.ZeroReturnError 為 e:

ProtocolError: ('Connection aborted.', OSError("(60, 'ETIMEDOUT')",))

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

您不需要使用 fake_useragent,只需嘗試一些這樣的...通過代理 cookies 變量來請求

import requests

url = "https://www.bestbuy.com/site/lg-65-class-oled-b9-series-2160p-smart-4k-uhd-tv-with-hdr/6360611.p?skuId=6360611"
agent = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}
cookies = {"cookie":"COPY_HERE_YOUR_COOKIE_FROM_BROWSER"}
url_get = requests.get(url,headers=agent, cookies=cookies)
print(url_get.text)

如果您不知道如何獲取 cookies,只需在瀏覽器中單擊右鍵(Chrome 示例)-> 檢查 > 網絡...當您加載 web 時,請查看第一個請求並查看標頭。 這段代碼對我有用。

暫無
暫無

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

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