python 爬虫 bs4 错误bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested

今天孩子学习爬虫练习,遇到如下错误:

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

    soup = bs4.BeautifulSoup(response.text, "lxml")
  File "E:\乐乐python\venv\lib\site-packages\bs4\__init__.py", line 251, in __init__
    % ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

代码如下:

# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/9/3 12:01
# 文件名称: 网页新闻.py
# 开发工具: PyCharm

import requests, bs4, time
response = requests.get("https://icourse.xesimg.com/programme/static/py/pcdata/lw-web/新闻网站/index.html")
response.encoding = "UTF-8"
soup = bs4.BeautifulSoup(response.text, "lxml")
data1 = soup.find_all(name="div", attrs={'class': 'article'})

for n in data1:
    data2 = n.find_all(name="a")
    print("--------------------------------------")
    print("题目:"+data2[0].text)
    print("摘要:"+data2[1].text)
    print("主题:"+data2[2].text)

提示soup = bs4.BeautifulSoup(response.text, "lxml")这句有误,xlml修改为html.parser

如下

soup = bs4.BeautifulSoup(response.text, "html.parser")

运行结果:

 正常

原因:没有解析库安装包,还有第二种方法,不改原码,安装下html包就行了

pip install lxml

import requests, bs4, time
response = requests.get("https://icourse.xesimg.com/programme/static/py/pcdata/lw-web/新闻网站/index.html")
response.encoding = "UTF-8"
soup = bs4.BeautifulSoup(response.text, "lxml")
data1 = soup.find_all(name="div", attrs={'class': 'article'})

for n in data1:
    data2 = n.find_all(name="a")
    print("--------------------------------------")
    print("题目:"+data2[0].text)
    print("摘要:"+data2[1].text)
    print("主题:"+data2[2].text)

运行也正常

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信息化未来

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值