分类: python3

beautifulsoup4 安装注意事项

输入命名如下:

pip3 install beautifulsoup4

在写代码的时候会出现一个错误如下:

Traceback (most recent call last): File "D:/pythoncode/爬虫/test.py", line 20, in <module> soup = BeautifulSoup(html, 'lxml') File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\__init__.py", line 243, in __init__ raise FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

这句话的意思是缺lxml这个包,安装即可,安装命令如下:

pip3 install lxml

html='''
<div class="panel">
    <div class="panel‐heading">
        <h4>Hello</h4>
    </div>
    <div class="panel‐body">
        <ul class="list" id="list‐1">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
            <li class="element">Jay</li>
        </ul>
        <ul class="list list‐small" id="list‐2">
            <li class="element">Foo</li>
            <li class="element">Bar</li>
        </ul>
    </div>
</div>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
for li in soup.select('li'):
    print(li.get_text())

安装了beautifulsoup4和lxml这两个包之后就可以正常执行和显示结果了 显示如下: Foo Bar Jay Foo Bar