python - beautifulsoup 无法识别 lxml

标签 python beautifulsoup lxml

我正在尝试使用 lxml 作为 BeautifulSoup 的解析器,因为默认的解析器要慢得多,但是我收到了这个错误:

    soup = BeautifulSoup(html, "lxml")
  File "/home/rob/python/stock/local/lib/python2.7/site-packages/bs4/__init__.py", line 152, 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?

lxml和beautifulsoup我都卸载重装了很多次了,还是不能读取。我也尝试重新安装 lxml 依赖项,但我仍然得到这个。

我什至创建了一个新的虚拟环境并重新安装了所有东西,但仍然出现此错误。

有人知道这里发生了什么吗?

编辑

在 ubuntu 桌面上的 Python 2.7.x 上使用最新版本的 bs4 和 lxml

我可以 import lxml 但我不能 from lxml import etree 返回:

  File "<stdin>", line 1, in <module>
ImportError: /usr/lib/x86_64-linux-gnu/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by /home/rob/python/stock/local/lib/python2.7/site-packages/lxml/etree.so)

我有 libxml,但我不确定版本,但我安装并重新安装了最新版本。也试过手动安装 2.9.0 还是不行

最佳答案

看起来 lxml 没有安装成功。要在 Ubuntu 上安装 lxml,请运行

sudo apt-get install libxslt1-dev libxml2

在虚拟环境中:

pip install --upgrade lxml
pip install cssselect

关于python - beautifulsoup 无法识别 lxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21322948/

相关文章:

python - 验证列内容时解析表元素的 lxml 语法错误

Python 函数调用中参数传递的排序规则

使用 beautifulsoup 对表进行 Python 迭代仅给出第一列

python - 如何使用 python-twitter 下载 pandas dataframe 中的 url 内容?

python - 解析嵌入的CSS beautifulsoup

javascript - 从脚本中的 var 中提取数据并使用 python 将 pdf 下载到文件夹

python - 获取 lxml/Python 中选定元素旁边的文本

python - 是否在 tornado.web.RequestHandler 中初始化,每次请求时都会被调用/

python - pandas:groupby 两列 nunique

python - Matplotlib 关闭绘图中文本的抗锯齿功能?