【问题标题】:Avoid to display the UserWarning message of bs4 library避免显示 bs4 库的 UserWarning 消息
【发布时间】:2016-09-06 17:19:49
【问题描述】:

每次我在python中用bs4将soup制作成页面的源代码时,终端都会显示:

/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))

有没有办法避免显示这个?

【问题讨论】:

  • 您是否尝试按照警告提示您执行的操作?
  • 是的...但在不同的计算机上可能是不同的警告...我需要禁用它。这是可选的。
  • 警告是一样的......它只能改变“html5ib”的一部分......仅此而已

标签: python python-3.x beautifulsoup warnings bs4


【解决方案1】:

虽然包含解析器是可选的,但警告会告诉您,如果您没有明确说明要使用哪个解析器,结果可能会因系统而异。

documentation 清楚地表明每个解析器都有优点和缺点。如果您让模块在系统上选择“最佳可用”,您可以在一个系统上使用html5lib,在另一个系统上使用html.parser。两者可以以不同的方式解析页面。此警告告诉您这是一种可能性。

点击文档查看大图

要修复您的警告,并确保所有系统都以相同的方式解析,explicitly 设置您要使用的解析器:

BeautifulSoup(html, "html5lib")

记住:

显式优于隐式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    相关资源
    最近更新 更多