【问题标题】:Handling Network Errors Python, Web Crawler处理网络错误 Python,网络爬虫
【发布时间】:2017-07-10 01:11:01
【问题描述】:

我正在大型网站上制作网络爬虫。但是,由于连接不稳定,它总是会遇到关闭连接、SSL 错误或其他间歇性故障。因此,我正在寻找一种方法来处理这个问题。这是我下面的代码,谁能告诉我如何实现等待或在有网络连接时再次尝试重新启动项目

try:
     requests.get("http://example.com")
except requests.exceptions.RequestException:
     pass  # handle the exception. maybe wait and try again later

【问题讨论】:

    标签: python web-scraping beautifulsoup web-crawler network-connection


    【解决方案1】:

    无需尝试侦听网络接口本身,您可以在失败时添加一个简单的“重试”机制:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      相关资源
      最近更新 更多