处理网络错误Python、Web爬虫
我正在一个大型网站上制作一个网络爬虫。然而,由于连接不稳定,它总是会遇到关闭连接、SSL错误或其他间歇性故障。因此,我正在寻找一种方法来解决这个问题。这是我下面的代码,有人能告诉我如何实现等待或在有网络连接时再次尝试启动项目吗处理网络错误Python、Web爬虫,python,web-scraping,beautifulsoup,web-crawler,network-connection,Python,Web Scraping,Beautifulsoup,Web Crawler,Network Connection,我正在一个大型网站上制作一个网络爬虫。然而,由于连接不稳定,它总是会遇到关闭连接、SSL错误或其他间歇性故障。因此,我正在寻找一种方法来解决这个问题。这是我下面的代码,有人能告诉我如何实现等待或在有网络连接时再次尝试启动项目吗 try: requests.get("http://example.com") except requests.exceptions.RequestException: pass # handle the exception. maybe wait
try:
requests.get("http://example.com")
except requests.exceptions.RequestException:
pass # handle the exception. maybe wait and try again later
无需尝试侦听网络接口本身,您可以在网络接口失败时添加一个简单的“重试”机制:
import time
while True:
try:
requests.get("http://example.com")
break # you can also check the returned status before breaking the loop
except requests.exceptions.RequestException:
time.sleep(300) # wait 5 mins before retry