from bs4 import BeautifulSoup as bs
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = bs(html_doc)
print(soup.prettify())
运行时报如下错误:
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.
翻译:UserWarning:没有明确指定解析器,因此我正在使用该系统的最佳可用HTML解析器(“lxml”)。 这通常不是问题,但是如果您在另一个系统或不同的虚拟环境中运行此代码,它可能会使用不同的分析器并且行为不同。
解决方法:
soup = bs(html_doc, "html.parser")
print(soup.prettify())