2016-09-06 102 views
-1

的UserWarning消息每次我做的湯頁面與蟒蛇BS4一個源代碼,終端顯示:避免顯示BS4庫

/usr/local/lib/python3.4/dist-packages/bs4/__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. 

The code that caused this warning is on line 229 of the file HibernetBlock.py. To get rid of this warning, change code that looks like this: 

BeautifulSoup([your markup]) 

to this: 

BeautifulSoup([your markup], "html5lib") 

    markup_type=markup_type)) 

有沒有辦法避免以顯示此?

+0

您是否試過做警告要求您做的事情? – Andy

+0

是啊...但它可以是一個不同的警告在另一臺計算機...我需要禁用它。這是可選的。 – Sperly1987

+0

警告是一樣的......它可以改變只是「html5ib」的一部分..沒有更多 – Sperly1987

回答

1

雖然包含解析器是可選的,但是警告告訴您,如果您沒有明確說明要使用哪個解析器,結果可能因系統而異。

documentation表明每個解析器都有優缺點。如果讓模塊在系統上選擇「最佳可用」,則可以在一個系統上使用html5lib,在另一個系統上使用html.parser。這兩者可以以不同的方式解析頁面。這個警告告訴你這是一種可能性。

BS Parsers 點擊查看大圖從文檔

要解決你的警告,並確保所有系統正在分析以同樣的方式,explicitly設定您想要使用的解析器:

BeautifulSoup(html, "html5lib") 

並記住:

顯式比隱式更好。