我正在尝试抓取网站,但收到以下错误:
('Connection aborted.', OSError("(60, 'ETIMEDOUT')",))
我试图在response.get()中更改超时= None,但是我仍然收到错误。
鉴于以下代码,我应如何处理上述错误?出于某种原因,此错误仅在我尝试抓取该网址时出现,其他所有内容对我来说都很好。
根据this question,建议使用以下代码。
# Import packages
import requests
from bs4 import BeautifulSoup
#Input URL
url = "https://www...."
# Try requests.get()
try:
r = requests.get(url)
except requests.ConnectionError as e:
print("OOPS!! Connection Error. Make sure you are connected to Internet. Technical Details given below.\n")
print(str(e))
except requests.Timeout as e:
print("OOPS!! Timeout Error")
print(str(e))
except requests.RequestException as e:
print("OOPS!! General Error")
print(str(e))
except KeyboardInterrupt:
print("Someone closed the program")
我也尝试了一种重试机制,但是如建议here那样,没有成功:
for i in range(0,10):
while True:
try:
r = requests.get(url)
except requests.ConnectionError as e:
print("OOPS!! Connection Error. Make sure you are connected to Internet. Technical Details given below.\n")
print(str(e))
continue
break