BeautifulSoup4 UserWarning /不影响程序运行

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.



To get rid of this warning, change this:


 BeautifulSoup([your markup])


to this:


 BeautifulSoup([your markup], "lxml")


  markup_type=markup_type))

 

 

 

解决办法,初始化时,加上解析器类型,常用解析器如下:

 

解析器使用方法优势劣势
Python标准库 BeautifulSoup(markup, "html.parser")
  • Python的内置标准库
  • 执行速度适中
  • 文档容错能力强
  • Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
lxml HTML 解析器 BeautifulSoup(markup, "lxml")
  • 速度快
  • 文档容错能力强
  • 需要安装C语言库
lxml XML 解析器

BeautifulSoup(markup, ["lxml", "xml"])

BeautifulSoup(markup, "xml")

  • 速度快
  • 唯一支持XML的解析器
  • 需要安装C语言库
html5lib BeautifulSoup(markup, "html5lib")
  • 最好的容错性
  • 以浏览器的方式解析文档
  • 生成HTML5格式的文档
  • 速度慢
  • 不依赖外部扩展

或者按照提示修改也可以,不影响程序运行

 

posted on 2017-08-04 15:11  ""  阅读(985)  评论(0编辑  收藏  举报