2015-11-04 126 views
26

我安裝了BeautifulSoup後,每當我在cmd中運行我的Python時,這個警告就會出現。如何擺脫BeautifulSoup用戶警告?

D:\Application\python\lib\site-packages\beautifulsoup4-4.4.1-py3.4.egg\bs4\__init__.py:166: 
UserWarning: No parser was explicitly specified, so I'm using the best 
available HTML parser for this system ("html.parser"). 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. 

To get rid of this warning, change this: 

BeautifulSoup([your markup]) 

to this: 

BeautifulSoup([your markup], "html.parser") 

我不知道它爲什麼出來以及如何解決它。

+8

的消息告訴你該怎麼做:'BeautifulSoup([你的標記], 「html.parser」)'。你是否這樣做,看看你的輸出是什麼? BeautifulSoup正在努力讓你的生活更輕鬆。聽湯。 :) – idjaw

+1

把'soup = BeautifulSoup(html)'的代碼改成'soup = BeautifulSoup(html,「html.parser」)'。 –

+0

謝謝大家! :D – jellyfishhuang

回答

51

您的問題的解決方案在錯誤消息中明確說明。像下面的代碼沒有指定XML/HTML /等。解析器。

BeautifulSoup(...) 

爲了修正這個錯誤,你需要指定你想使用,像這樣該解析器:

BeautifulSoup(..., "html.parser") 

您還可以,如果你」安裝第三方解析器喜歡。

+0

謝謝!解決了。 – jellyfishhuang

+0

太好了!有用 –

10

文檔建議您安裝並使用lxml來提高速度。

BeautifulSoup(html, "lxml") 

如果您正在使用Python版本2早於2.7.3,或版本 的Python 3早於3.2.2的,重要的是你安裝LXML 或html5lib-Python的內置非常必要-HTML解析器在 舊版本中並不是很好。

安裝LXML解析器

  • 在Ubuntu(Debian的)

    apt-get install python-lxml 
    
  • 的Fedora(RHEL基於)

    dnf install python-lxml 
    
  • 使用PIP

    pip install lxml