1. 程式人生 > 實用技巧 >bs4 FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml----轉載

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

bs4 FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

安裝beautifulsoup後,執行測試報錯

from urllib import request
from bs4 import BeautifulSoup

url = "http://www.baidu.com"
rsp = request.urlopen(url)
content = rsp.read()

soup = BeautifulSoup(content, "lxml")

print(soup.title.string)
----------------------------------------------------------------------
FeatureNotFound Traceback (most recent call last)
~/data/spider/bs.py in <module>
16 content = rsp.read()
17
---> 18 soup = BeautifulSoup(content, "lxml")
19
20 print(soup.title.string)

~/data/spider/venv/lib/python3.7/site-packages/bs4/__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs)
196 "Couldn't find a tree builder with the features you "
197 "requested: %s. Do you need to install a parser library?"
--> 198 % ",".join(features))
199 builder = builder_class()
200 if not (original_features == builder.NAME or

FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

經過測試發現是lxml的問題,使用 pip install lxml 安裝,安裝完後,在執行還是一樣出錯, 經百度、知乎的說法是新的庫不支援,新版本語法支援改變了

使用 pip install lxml時,自動安裝的是最新 4.2.5版本

解決方法一:

  解除安裝新的換老的就可以了

    pip uninstall lxml

    pip install lxml==3.7.0

方法二:

 在報錯程式碼中把函式引數中所有的"lxml"改成"html.parser"
soup = BeautifulSoup(content, "lxml")
改成 soup = BeautifulSoup(content, "html.parser")