1 回答

TA貢獻1827條經驗 獲得超8個贊
您正在使用 try 塊處理異常并且什么都不做(只是pass),這就是您沒有看到錯誤消息的原因。如果發生不在 try 塊內的錯誤,默認行為是中斷代碼并打印堆棧跟蹤。如果在 try 塊內發生錯誤,代碼將跳轉到 except 塊,接下來發生什么由您決定。不會自動打印錯誤信息。
如果您嘗試打印錯誤或在循環內添加 Soup 對象的打印語句,您將看到以下內容:
try:
soup = BeautifulSoup(cont, "html.parser")
print('Now')
# Print the soup object
print(soup)
if soup.findAll('title')[0].get_text() is None:
print('Hi')
print('after if')
#print(element.ljust(element_length), resp.status_code, soup.find('title').text)
except Exception as error:
# Handle the exception with some information.
print(error)
pass
給出輸出
Sorry, we are unable to process your request at this time.
對于打印語句,錯誤消息如下所示:
list index out of range
基本上,您無法解析 URL,因此您嘗試使用[0]if 語句中的訪問空數組,這會引發錯誤。
添加回答
舉報