【问题标题】:try/catch not catching HTTPError [duplicate]尝试/捕获未捕获 HTTPError [重复]
【发布时间】:2020-12-16 18:40:59
【问题描述】:

我有一些基本代码尝试使用 BeautifulSoup 请求页面:

from bs4 import *
from urllib.request import urlopen

try:
    soup = BeautifulSoup(urlopen('http://badsite.com/404'))
except urllib.exceptions.HTTPError:
    pass

这仍然引发:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-24-a0aa8eb7a9ee> in <module>

我已将urllib.exceptions.HTTPError 替换为requests.HTTPErrorrequests.exceptions.HTTPError。他们所有人仍然无法捕捉到错误。如何正确捕获 HTTPError?

【问题讨论】:

    标签: python beautifulsoup try-catch


    【解决方案1】:

    你试过urllib.error.HTTPError吗? This 回答可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-07-16
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      相关资源
      最近更新 更多