【问题标题】:how do you skip a request and move to the next requests if the website is not responding? (python)如果网站没有响应,您如何跳过请求并转到下一个请求? (Python)
【发布时间】:2022-08-23 17:30:50
【问题描述】:

如果网站没有响应,您如何跳过请求并转到下一个请求?

for website in websites:
  try:
    response = requests.get(website, timeout=5)
  except:
    pass

    标签: python request


    【解决方案1】:

    只需检查响应的状态代码。如果是 200 那么你就是金子,如果是 404 close 那么意味着请求失败。

    for website in websites:
        try:
            response = requests.get(website, timeout=5)
        except:
            return
        if resoponse.status_code != 200:
            # .... do something
        else:
             # .... do something else.
       
    

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 2015-08-18
      • 2016-03-30
      • 1970-01-01
      • 2021-12-12
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多