bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.

在写代码时

html ="""
<html><head><title> The Dormouse's story</title></head>
<body>
<p class= "title" name= "dromouse"><b>The Dormouse's story</b></p>
<p class= "story">0nce 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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,'lxml')
print(soup.prettify())
print(soup.title.string)

当遇到如图1所示的问题时bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?不要慌,这个问题很好解决。
 


在这里插入图片描述
图1

bs4调用了python自带的html解析器,而这段代码中采用的解析器确实lxml,所以会出现报错。解决方法:我的是采用的Pycharm IDE所以直接就在setting里面安装了lxml库,也可以在DOS命令中,指定位置通过pip命令进行安装。

猜你喜欢

转载自blog.csdn.net/weixin_42555080/article/details/87869421