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"
html.parser
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具