2015-11-04 163 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