1. 程式人生 > >BeautifulSoup庫未寫明解析器警告

BeautifulSoup庫未寫明解析器警告

lib ping 參數 warn () have addition 一個 different

from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen("http://www.pythonscraping.com/pages/page1.html") bsObj = BeautifulSoup(html.read()) print(bsObj.h1)

代碼運行之後警告如下:
UserWarning: No parser was explicitly specified, so I‘m using the best available HTML parser for this system ("lxml"). 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 4 of the file D:/Python/venv/test8.py. To get rid of this warning, pass the additional argument ‘features="lxml"‘ to the BeautifulSoup constructor.

翻譯如下:
用戶警告:沒有顯式指定語法分析器,因此我使用了此系統的最佳可用HTML語法分析器(“lxml”)。這通常不是問題,但是如果您在另一個系統上運行此代碼,或者在不同的虛擬環境中運行此代碼,它可能會使用不同的解析器並表現出不同的行為。

導致此警告的代碼位於文件d:/python/venv/test8.py的第4行。要消除此警告,請將附加參數‘features=“lxml”‘傳遞給beautifulsoup構造函數。

解決:指定解析器,一般使用‘lxml‘

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj = BeautifulSoup(html.read(),‘lxml‘)
print(bsObj.h1)

BeautifulSoup庫未寫明解析器警告