Python中lxml模組的安裝

2021-08-10 20:11:21 字數 2190 閱讀 4952

本文**:這裡,感謝原文作者的分享,親測有效,希望可以幫到更多的童鞋。如有侵權,請告知。

lxml是python中與xml及html相關功能中最豐富和最容易使用的庫。lxml並不是python自帶的包,而是為libxml2和libxslt庫的乙個python化的繫結。它與眾不同的地方是它兼顧了這些庫的速度和功能完整性,以及純python api的簡潔性,與大家熟知的elementtree api相容但比之更優越!但安裝lxml卻又有點麻煩,因為存在依賴,直接安裝的話用easy_install, pip都不能成功,會報gcc錯誤。下面列出來windows、linux下面的安裝方法:

【windows系統】

先確保python已經安裝好,環境變數也配置好了,相應的的easy_install、pip也安裝好了.

1. 執行 pip install virtualenv

[python]

view plain

copy

c:\>pip install virtualenv  

requirement already satisfied (use --upgrade to upgrade): virtualenv in

c:\python27\lib\site-package  

s\virtualenv-12.0.4

-py2.

7.egg

note:

[python]

view plain

copy

lxml-

2.3-py2.

7-win-amd64.egg (md5)     

# python egg

或  lxml-2.3

.win-amd64-py2.

7.exe

(md5)     

# ms windows installer 3.

執行 easy_install lxml-2.3-py2.7-win-amd64.egg

系統會自動為我們安裝lxml需要的依賴。

[python]

view plain

copy

d:\downloads>easy_install lxml-

2.3-py2.

7-win-amd64.egg    

# 進入該檔案所在目錄執行該命令

processing lxml-2.3

-py2.

7-win-amd64.egg  

creating c:\python27\lib\site-packages\lxml-2.3

-py2.

7-win-amd64.egg  

extracting lxml-2.3

-py2.

7-win-amd64.egg to c:\python27\lib\site-packages  

adding lxml 2.3

to easy-install.pth file  

installed c:\python27\lib\site-packages\lxml-2.3

-py2.

7-win-amd64.egg  

processing dependencies for

lxml==

2.3finished processing dependencies for

lxml==

2.3

note:

1. 可用exe可執行檔案,方法更簡單直接安裝就可以

2. 可用easy_install安裝方式,也可以用pip的方式

[python]

view plain

copy

#再執行下,就安裝成功了!

>>> import

lxml     

>>>   

3. 如用pip安裝,常用命令就是:4.

【linux系統

】因為lxml依賴的包如下:

libxml2, libxml2-devel, libxlst, libxlst-devel, python-libxml2, python-libxslt

所以安裝步驟如下:

第一步: 安裝 libxml2

第二步: 安裝 libxslt

第三步: 安裝 python-libxml2 和 python-libxslt

第四步: 安裝 lxml

參考官方文件:

Python中lxml模組的安裝

lxml是python中與xml及html相關功能中最豐富和最容易使用的庫。lxml並不是python自帶的包,而是為libxml2和libxslt庫的乙個python化的繫結。它與眾不同的地方是它兼顧了這些庫的速度和功能完整性,以及純python api的簡潔性,與大家熟知的elementtree...

Python 之lxml解析模組

lxml 是 乙個html xml的解析器,主要的功能是如何解析和提取 html xml 資料。一 lxml示例 1 初步 使用 lxml 的 etree 庫 from lxml import etree text 利用etree.html,將字串解析為html文件 html etree.html ...

lxml模組的使用

安裝 pip install lxml 匯入lxml 的 etree 庫 匯入沒有提示不代表不能用 from lxml import etree 利用etree.html,將字串轉化為element物件,element物件具有xpath的方法,返回結果的列表,能夠接受bytes型別的資料和str型別...