尝试/捕获不捕获HTTPError

时间:2020-08-27 20:06:26

标签: python beautifulsoup try-catch

我有一些基本代码尝试使用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?

1 个答案:

答案 0 :(得分:1)

您尝试过 PO Material. Material Desc.Amount_USD PO_Pur_Org Count Total 0 6001488.0 SCRAP: Light (L/S) 485.33300 SA02 4624 2.244180e+06 1 6001488.0 SCRAP: Light (L/S) 728.00000 SA02 1888 1.374464e+06 2 6001488.0 SCRAP: Light (L/S) 242.66700 SA02 1778 4.314619e+05 3 6001488.0 SCRAP: Light (L/S) 606.66700 SA02 1044 6.333603e+05 吗? This的答案可能会有帮助。