allexj allexj - 2 months ago 20
Python Question

Avoid to display the UserWarning message of bs4 library

Every time I make soup a sourcecode of a page with bs4 in python, the terminal shows:

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


Is there a way to avoid to display this?

Answer

While the inclusion of the parser is optional, the warning is telling you that results may vary from system to system if you don't explicitly state which parser you want to use.

The documentation makes it clear that there are advantages and disadvantages to each parser. If you let the module choose "best available" on the system, you may use html5lib on one system and html.parser on another. The two could parse a page differently. This warning is telling you that is a possibility.

BS Parsers Click for larger view from documentation

To fix your warning, and ensure that all system are parsing the same way, explicitly set the parser you want to use:

BeautifulSoup(html, "html5lib")

And remember:

Explicit is better than implicit.

Comments