2015-10-20 20 views
1

我收到Beutifulsoup HTMLParseError: expected name token at u'<![0Y', at line 1371, column 24的異常 - 這是因爲我閱讀的html格式不正確。捕獲beautifulsoup HTMLParseError異常

如何抓住這個錯誤 - 我曾嘗試

try: 
    ... 
except HTMLParseError: 
    pass 

但導致錯誤NameError: global name 'HTMLParseError' is not defined

我也曾嘗試except BeautifulSoup.HTMLParseError:但隨後給出了錯誤AttributeError: type object 'BeautifulSoup' has no attribute 'HTMLParseError'

更廣泛地,當我從我正在使用的包中得到一個自定義錯誤時,如何「解決」需要處理它的異常?

回答

2

BeautifulSoup從HTMLParser的庫提高HTMLParseError。嘗試在你的try /使用它,除非之前導入該庫中的錯誤:在HTMLParse庫

from HTMLParser import HTMLParseError 

try: 
    # error happens 
except HTMLParseError: 
    pass 

更多信息是here

查看BeautifulSoup源代碼here中出現錯誤的位置。

-2

你有沒有嘗試catch NameError異常?

,如果你不能抓住它試試這個:

try: 
    # error happens 
except Exception as e: 
    # log the exception here 
    print(e) 
+1

我認爲'NameError'異常只是因爲'除了HTMLParseError'是無效的,所以檢查這不是一個真正的解決方案。我還希望避免「全面解決方案」。 – kyrenia