【问题标题】:How to re-install lxml?如何重新安装lxml?
【发布时间】:2013-07-19 22:58:25
【问题描述】:

使用的 Python 版本和设备

  • Python 2,7.5
  • Mac 10.7.5
  • BeautifulSoup 4.2.1.

我正在关注 BeautifulSoup 教程,但是当我尝试使用 lxml 库解析 xml 页面时,出现以下错误:

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

我确定我已经通过所有方法安装了 lxml:easy_install、pip、port 等。我尝试在我的代码中添加一行以查看是否安装了 lxml:

import lxml

那么python就可以成功地遍历这段代码并再次显示之前的错误消息,发生在同一行。

所以我很确定 lxml 已安装,但未正确安装。所以我决定卸载 lxml,然后使用“正确”的方法重新安装。但是当我输入

easy_install -m  lxml

我收到以下错误:

Searching for lxml
Best match: lxml 3.2.1
Processing lxml-3.2.1-py2.7-macosx-10.6-intel.egg

Using /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-
3.2.1-py2.7-macosx-10.6-intel.egg

Because this distribution was installed --multi-version, before you can
import modules from this package in an application, you will need to
'import pkg_resources' and then use a 'require()' call similar to one of
these examples, in order to select the desired version:

pkg_resources.require("lxml")  # latest installed version
pkg_resources.require("lxml==3.2.1")  # this exact version
pkg_resources.require("lxml>=3.2.1")  # this version or higher

Processing dependencies for lxml
Finished processing dependencies for lxml

所以我不知道如何继续卸载,我在 google 上查找了很多关于此问题的帖子,但仍然找不到任何有用的信息。


这是我的源代码

import mechanize
from bs4 import BeautifulSoup
import lxml

class count:
    def __init__(self,protein):
        self.proteinCode = protein
        self.br = mechanize.Browser()

    def first_search(self):
        #Test 0
        soup = BeautifulSoup(self.br.open("http://www.ncbi.nlm.nih.gov/protein/21225921?report=genbank&log$=prottop&blast_rank=1&RID=YGJHMSET015"), ['lxml','xml'])
        return

if __name__=='__main__':
    proteinCode = sys.argv[1]
    gogogo = count(proteinCode)

问题

  1. 如何卸载 lxml?
  2. 如何“正确”安装 lxml?我如何知道它已正确安装?

【问题讨论】:

    标签: python web-scraping beautifulsoup lxml easy-install


    【解决方案1】:

    FWIW,我遇到了类似的问题(python 3.6,os x 10.12.6)并且能够通过简单地解决它(第一个命令只是表示我在 conda virtualenv 中工作):

    $ source activate ml-general
    $ pip uninstall lxml
    $ pip install lxml
    

    我首先尝试了更复杂的事情,因为 BeautifulSoup 可以通过 Jupyter+iPython 使用相同的命令正常工作,但不能通过同一 virtualenv 中的 PyCharm 终端。只需按照上述方式重新安装 lxml 即可解决问题。

    【讨论】:

      【解决方案2】:

      如果您在 Ubuntu/Debian 中使用 Python2.7,这对我有用:

      $ sudo apt-get build-dep python-lxml
      $ sudo pip install lxml 
      

      像这样测试它:

      mona@pascal:~/computer_vision/image_retrieval$ python
      Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
      [GCC 4.8.2] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import lxml
      

      【讨论】:

        【解决方案3】:

        apt-get 在 Debian/Ubuntu 上: sudo apt-get install python3-lxml 对于 MacOS-X,可以使用 lxml 的 macport。尝试类似的东西 sudo port install py27-lxml

        http://lxml.de/installation.html 可能会有所帮助。

        【讨论】:

          【解决方案4】:

          我正在使用 BeautifulSoup 4.3.2 和 OS X 10.6.8。我也有安装不正确的问题lxml。以下是我发现的一些事情:

          首先,检查这个相关问题:Removed MacPorts, now Python is broken

          现在,为了检查安装了 BeautifulSoup 4 的构建器,请尝试

          >>> import bs4
          >>> bs4.builder.builder_registry.builders
          

          如果您没有看到您喜欢的构建器,则说明它没有安装,您将看到如上的错误(“找不到树构建器...”)。

          另外,仅仅因为你可以import lxml,并不意味着一切都是完美的。

          试试

          >>> import lxml
          >>> import lxml.etree
          

          要了解发生了什么,请转到 bs4 安装并打开 egg (tar -xvzf)。注意模块bs4.builder。在其中您应该看到诸如_lxml.py_html5lib.py 之类的文件。所以你也可以试试

          >>> import bs4.builder.htmlparser
          >>> import bs4.builder._lxml
          >>> import bs4.builder._html5lib
          

          如果出现问题,您会看到为什么无法加载特定模块。您会注意到在builder/__init__.py 的末尾它如何加载所有这些模块并忽略未加载的任何内容:

          # Builders are registered in reverse order of priority, so that custom
          # builder registrations will take precedence. In general, we want lxml
          # to take precedence over html5lib, because it's faster. And we only
          # want to use HTMLParser as a last result.
          from . import _htmlparser
          register_treebuilders_from(_htmlparser)
          try:
              from . import _html5lib
              register_treebuilders_from(_html5lib)
          except ImportError:
              # They don't have html5lib installed.
              pass
          try:
              from . import _lxml
              register_treebuilders_from(_lxml)
          except ImportError:
              # They don't have lxml installed.
              pass
          

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-24
          • 2016-06-26
          • 2020-11-13
          • 2016-05-10
          • 2023-03-02
          • 2016-12-13
          相关资源
          最近更新 更多