導航:首頁 > 編程語言 > lxmpython

lxmpython

發布時間:2022-04-21 23:18:43

python lxml庫怎麼安裝

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 print?
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
2. 從官方網站下載與系統,Python版本匹配的lxml文件:
http //pypi.python.org/pypi/lxml/2.3/
NOTE:
比如說我的電腦是Python 2.7.4, 64位操作系統,那麼我就可以下載
[python] view plain print?
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
[python] view plain print?
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.3
Finished processing dependencies for lxml==2.3
NOTE:
1. 可用exe可執行文件,方法更簡單直接安裝就可以
2. 可用easy_install安裝方式,也可以用pip的方式
[python] view plain print?
#再執行下,就安裝成功了!
>>> import lxml
>>>
3. 如用pip安裝,常用命令就是:
pip install simplejson # 安裝Python包
pip install --upgrade simplejson # 升級Python包
pip uninstall simplejson # 卸載Python包
4. 如用Eclipse+Pydev的開發方式,需要移除舊包,重新載入一次
Window --> Preferences --> PyDev --> Interperter-python # 否則導包的時候會報錯
【Linux系統】
因為lxml依賴的包如下:
libxml2, libxml2-devel, libxlst, libxlst-devel, python-libxml2, python-libxslt
所以安裝步驟如下:
第一步: 安裝 libxml2
$ sudo apt-get install libxml2 libxml2-dev
第二步: 安裝 libxslt
$ sudo apt-get install libxlst libxslt-dev
第三步: 安裝 python-libxml2 和 python-libxslt
$ sudo apt-get install python-libxml2 python-libxslt
第四步: 安裝 lxml
$ sudo easy_install lxml

㈡ python中lxml模塊怎麼導入

這個模塊是第三方模塊,需要先安裝再導入。

安裝:終端命令界面下,pip install lxml(安裝過程中如果提示需要其他哪個庫,需要先裝提示的庫,再裝lxml)。
如果使用pip安裝失敗,到pypi社區官網下載壓縮解壓,終端界面進入其目錄(當前目錄有個叫「setup.py」就對了),用命令 python setup install 就行。

導入:import lxml 即可

㈢ python中lxml這個庫主要是干什麼的

1、lxml是XML和HTML的解析器,
2、其主要功能是解析和提取XML和HTML中的數據;
3、lxml和正則一樣,也是用C語言實現的,是一款高性能的python HTML、XML解析器,也可以利用XPath語法,來定位特定的元素及節點信息

㈣ python lxml etree怎麼甩

lxml是Python語言中處理XML和HTML功能最豐富,最易於使用的庫。

lxml是libxml2和libxslt兩個C庫的Python化綁定,它的獨特之處在於兼顧了這些庫的速度和功能完整性,同時還具有Python API的簡介。兼容ElementTree API,但是比它更優越。

用libxml2編程就像是一個異於常人的陌生人的令人驚恐的擁抱,它看上去可以滿足你一切瘋狂的夢想,但是你的內心深處一直在警告你,你有可能會以最糟糕的方式遭殃,所以就有了lxml。



這是一個用lxml.etree來處理XML的教程,它簡單的概述了ElementTree API的主要概念,同時有一些能讓你的程序生涯更輕松的簡單的提高。


首先是導入lxml.etree的方式:

fromlxmlimportetree

為了協助代碼的可移植性,本教程中的例子很明顯可以看出,一部分API是lxml.etree在ElementTree API(由Fredrik Lundh 的ElementTree庫定義)的基礎上的擴展。

Element是ElementTree API的主要容器類,大部分XML tree的功能都是通過這個類來實現的,Element的創建很容易:

root=etree.Element("root")

element的XML tag名通過tag屬性來訪問

>>>printroot.tag
root



許多Element被組織成一個XML樹狀結構,創建一個子element並添加進父element使用append方法:

>>>root.append(etree.Element("child1"))



還有一個更簡短更有效的方法:the SubElement,它的參數和element一樣,但是需要父element作為第一個參數:

>>>child2=etree.SubElement(root,"child2")
>>>child3=etree.SubElement(root,"child3")



可以序列化你創建的樹:

>>>print(etree.tostring(root,pretty_print=True))
<root>
<child1/>
<child2/>
<child3/>
</root>



為了更方便直觀的訪問這些子節點,element模仿了正常的Python鏈:

>>>child=root[0]>>>print(child.tag)
child1
>>>print(len(root))
>>>root.index(root[1])#lxml.etreeonly!
>>>children=list(root)>>>forchildinroot:...print(child.tag)child1child2
child3
>>>root.insert(0,etree.Element("child0"))>>>start=root[:1]>>>end=root[-1:]>>>print(start[0].tag)child0>>>print(end[0].tag)child3


還可以根據element的真值看其是否有孩子節點:

ifroot:#thisnolongerworks!
print("Therootelementhaschildren")


用len(element)更直觀,且不容易出錯:

>>>print(etree.iselement(root))#testifit'ssomekindofElement
True
>>>iflen(root):#testifithaschildren
...print("Therootelementhaschildren")
Therootelementhaschildren



還有一個重要的特性,原文的句子只可意會,看例子應該是能看懂什麼意思吧。

>>>forchildinroot:...print(child.tag)child0child1child2child3>>>root[0]=root[-1]#移動了element>>>forchildinroot:...print(child.tag)child3child1child2>>>l=[0,1,2,3]>>>l[0]=l[-1]>>>l[3,1,2,3]
>>>rootisroot[0].getparent()#lxml.etreeonly!.etree,'sstandardlibrary:>>>fromimportdeep>>>element=etree.Element("neu")>>>element.append(deep(root[1]))>>>print(element[0].tag)child1>>>print([c.tagforcinroot])['child3','child1','child2']



XML支持屬性,創建方式如下:

>>>root=etree.Element("root",interesting="totally")
>>>etree.tostring(root)
b'<rootinteresting="totally"/>'



屬性是無序的鍵值對,所以可以用element類似於字典介面的方式處理:

>>>print(root.get("interesting"))
totally
>>>print(root.get("hello"))
None
>>>root.set("hello","Huhu")
>>>print(root.get("hello"))
Huhu
>>>etree.tostring(root)
b'<rootinteresting="totally"hello="Huhu"/>'
>>>sorted(root.keys())
['hello','interesting']
>>>forname,valueinsorted(root.items()):
...print('%s=%r'%(name,value))
hello='Huhu'
interesting='totally'

如果需要獲得一個類似dict的對象,可以使用attrib屬性:

>>>attributes=root.attrib
>>>print(attributes["interesting"])
totally
>>>print(attributes.get("no-such-attribute"))
None
>>>attributes["hello"]="GutenTag"
>>>print(attributes["hello"])
GutenTag
>>>print(root.get("hello"))
GutenTag

既然attrib是element本身支持的類似dict的對象,這就意味著任何對element的改變都會影響attrib,反之亦然。這還意味著只要element的任何一個attrib還在使用,XML樹就一直在內存中。通過如下方法,可以獲得一個獨立於XML樹的attrib的快照:

>>>d=dict(root.attrib)
>>>sorted(d.items())
[('hello','GutenTag'),('interesting','totally')]

㈤ win7下安裝lxml使用python哪個版本

我是拷貝黨。不過裝個軟體都要用360什麼的輔助安裝,真的不適合搞軟體開發。適合做銷售。 ------------------------------------------------------------------------------------------------------------ 最近安裝了Windows 7,雖然沒有號稱的那麼強大的功能改善,但各方面還是有所提升。 而同時,由於畢設需要Python程序設計。於是就在Window 7下安裝Python了。 一開始我安裝了Python 3.1版本,但是不知道什麼原因,python指令可以運行,也就是說安裝是成功的。但是程序卻運行不了。 於是,還是老老實實的用Python 2.6吧,關於版本,我就不做過多說明了。 下面就開始安裝,首先要確認: 1. 你是window 7用戶; 2. 你想用Python語言做些東西,或者想試試「Hello World!」。 對於安裝來說,我比較推薦ActiveState的集成安裝包。進入ActivePython頁面,點擊Download Now。文件大概有33M左右,所以你可以休息一陣,看個電影。 下載完後,雙擊文件,然後一路Next就完成了安裝。非常簡單。 然後進入命令行(Win鍵+R,然後輸入cmd,然後回車),輸入python,就能進入python語言的編輯提示符。試一下print 「hello world!」吧。 如果出現hello world!輸出,就證明安裝成功了。 註:目前發現ActivePython帶的PythonWin編輯器不好使,即使用Windows XP的兼容模式也無濟於事。不過那個對我來說無所謂,Python的編輯器有好多,沒必要只用那個。

㈥ lxml在python中怎麼安裝

首先要有 Python :You need Python 2.3 or later.
然後是需要:You need libxml2 and libxslt, in particular:
使用:$sudo apt-get install libxml2 libxml2-dev 安裝 libxml2
使用:$sudo apt-get install libxlst libxslt-dev 安裝 libxslt
安裝 python-libxml2 和 python-libxslt :$sudo apt-get install python-libxml2 python-libxslt
然後就可以使用:$sudo easy_install lxml 來安裝最新的 lxml 了。我裝的是最新版本:lxml 2.2beta1
在 Cygwin 上安裝也一樣,直接選擇安裝 libxml2, libxml2-devel, libxlst, libxlst-devel, python-libxml2, python-libxslt 包安裝,然後 $sudo easy_install lxml 就可以裝上了!

㈦ Python lxml所有與多個命名空間問題,怎麼解決

有如下xml

<A xmlns="http://This/is/a/namespace">
<B>dataB1</B>
<B>dataB2</B>
<B>
<C>dataC</C>
</B>
</A>

其中的xmlns屬性表示的是該xml的默認命名空間,該命名空間必須是一個url形式
查看xml的tag

#encoding=utf8

from lxml import etree

str_xml = """
<A xmlns="http://This/is/a/namespace">
<B>dataB1</B>
<B>dataB2</B>
<B>
<C>dataC</C>
</B>
</A>
"""

xml = etree.fromstring(str_xml)
for node in xml.iter():
print node.tag

結果為:
{http://This/is/a/namespace}A
{http://This/is/a/namespace}B
{http://This/is/a/namespace}B
{http://This/is/a/namespace}B
{http://This/is/a/namespace}C

可以看到,跟普通xml的tag相比每個tag前面都多出了一個命名空間

獲取命名空間 .nsmap

from lxml import etree

str_xml = """
<A xmlns="http://This/is/a/namespace">
<B>dataB1</B>
<B>dataB2</B>
<B>
<C>dataC</C>
</B>
</A>
"""

xml = etree.fromstring(str_xml)
ns = xml.nsmap
print ns
print ns[None]

結果
{None: 'http://This/is/a/namespace'}
http://This/is/a/namespace

ns[None]獲取的是默認命名空間,ns會顯示所有的命名空間

獲取有命名空間的節點內容

from lxml import etree

str_xml = """
<A xmlns="http://This/is/a/namespace">
<B>dataB1</B>
<B>dataB2</B>
<B>
<C>dataC</C>
</B>
</A>
"""

xml = etree.fromstring(str_xml)
ns = xml.nsmap[None]
ns = "{%s}" % ns
for item in xml.findall("{0}B/{0}C".format(ns)): #不能用xpath會出錯
print item.text

結果
dataC

注意,在查找節點時,每一級節點都需要加上命名空間。而且測試時發現,findall可以正常查找到信息,而xpath會報錯。

獲取帶命名空間節點的屬性值

from lxml import etree

str_xml = """
<A xmlns="http://This/is/a/namespace">
<B b="123">dataB1</B>
<B>dataB2</B>
<B>
<C>dataC</C>
</B>
</A>
"""

xml = etree.fromstring(str_xml)
ns = xml.nsmap[None]
ns = "{%s}" % ns

item = xml.find(ns+"B")
print item.get("b")
print item.text

結果
123
dataB1

可以看到,獲取屬性時,不需要加命名空間,直接獲取即可

㈧ Python LXML模塊死活安裝不了怎麼辦

1、首先請確認安裝了xcode commond line tool 也就是xcode的命令行工具,因為編譯lxml需要用到cc 等命令
所以先在命令行輸入

xcode-select --install

2、安裝完命令行工具 用pip來安裝你所需要的lxml
這時候如果提示頭文件未找到的話 用locate命令定位一下頭文件的位置

locate xmlversion.h

3、初次使用locate命令會提示安裝

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

4、安裝一下就好了
之後定位到了locate之後 設置一下環境變數

export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/

5、那麼在編譯lxml的時候 就自動會到這個路徑下搜索頭文件
然後pip install 就可以啦。

6、另外如果系統裝了幾個版本的xcode 那麼列印看好xcode的路徑

sudo xcode-select -p

7、如果想切換到另一個xcode路徑下 可以考慮在bash.profile進行全局的設置
或者在終端設置變數

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/

或者

export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer

8、如果這些安裝完了之後,最後引用Scrapy包得時候報錯
請嘗試刪除
cd /Library/Python/2.7/site-packages/
sudo rm -rf six*

9、如果是Mac OSX 10.11 有可能會報錯 因為mac系統多了sip 關閉sip就OK了
具體關閉方法請網路
基本我就踩到了這些坑。

閱讀全文

與lxmpython相關的資料

熱點內容
單片機kill4軟體下載收費嗎 瀏覽:846
蘋果手機怎麼連接RMS伺服器 瀏覽:603
cisco路由器基本配置命令 瀏覽:186
android狀態欄顏色透明 瀏覽:116
db2編譯工具 瀏覽:181
騰訊雲伺服器創建環境 瀏覽:567
監管資金怎麼解壓 瀏覽:670
榮耀手機內存清理文件夾 瀏覽:175
外派程序員方案 瀏覽:554
安卓怎麼把桌面的軟體變成卡通 瀏覽:885
魯班鎖解壓嗎 瀏覽:395
打包發送文件如何加密 瀏覽:213
centos解壓縮zip 瀏覽:388
我的世界怎麼用命令風塊取消指令 瀏覽:1000
安卓軟體請求超時怎麼辦 瀏覽:476
androidapp調用另一個app 瀏覽:621
數控銑床法蘭克子程序編程 瀏覽:174
linux打包命令targz 瀏覽:997
抖音app是哪個 瀏覽:407
蘋果app怎麼上架 瀏覽:255