【问题标题】:How do I skip if the response is 404 using python requests? [duplicate]如果使用 python 请求响应为 404,我该如何跳过? [复制]
【发布时间】:2021-10-16 08:33:46
【问题描述】:
for user in comments['comment_id']:
  try:
    get_profile(user)
  except urllib.error.HTTPError:
    continue

我遇到了这样的错误,我不知道如何解决这个问题

<ipython-input-38-c9a67ae2e3f1> in <module>()
      2 for user in comments['comment_id']:
      3   try:
----> 4     get_profile(user)
      5   except urllib.error.HTTPError:
      6     continue

/usr/local/lib/python3.7/dist-packages/requests/models.py in raise_for_status(self)
    939 
    940         if http_error_msg:
--> 941             raise HTTPError(http_error_msg, response=self)
    942 
    943     def close(self):

HTTPError: 404 Client Error: Not Found for url: https://m.facebook.com/344608967148594/about/

【问题讨论】:

  • 什么是get_profile?是否使用requests 模块?因为看起来您只是捕获了错误的 HTTPError 类。

标签: python python-requests http-status-code-404


【解决方案1】:
for user in comments['comment_id']:
  try:
    get_profile(user)
  except:
    continue

【讨论】:

  • 虽然这有效,但它会捕获 所有 异常,而不仅仅是 404 响应。捕获特定异常是最佳实践。
  • 也许你可以试试这个:stackoverflow.com/questions/3193060/…我希望它有帮助
猜你喜欢
  • 1970-01-01
  • 2021-10-01
  • 2011-01-15
  • 2022-08-23
  • 2016-11-19
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2014-12-08
相关资源
最近更新 更多