• 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")
  • 相关阅读:
    洛谷 P1879 [USACO06NOV]玉米田Corn Fields
    洛谷 P2709 小B的询问
    洛谷 P1972 [SDOI2009]HH的项链
    洛谷 P3648 [APIO2014]序列分割
    洛谷 P2157 [SDOI2009]学校食堂
    洛谷 P1198 [JSOI2008]最大数
    洛谷 P3870 [TJOI2009]开关
    【模板】线段树2
    【模板】线段树1
    git之远程标签下载(远程分支)
  • 原文地址:https://www.cnblogs.com/lnn123/p/13525756.html
Copyright © 2020-2023  润新知