在Linux服務器上安裝lxml
最近新接的活,第一個任務是處理一堆xml格式的專利文件,把裏面的有效信息提取出來
因為公司的相關規定不允許把文件down到本地處理,只能在對方提供的遠程服務器上寫代碼
由於xml裏面的元素是XXX:YYYY這種帶前綴的格式,用xml.etree的ElementTree死活解析不出來,最後從OverStack上找到了解釋
ElementTree is not too smart about namespaces. You need to give the .find()
, findall()
and iterfind()
methods an explicit namespace dictionary. This is not documented very well:
namespaces = {‘owl‘: ‘http://www.w3.org/2002/07/owl#‘} # add more as needed
root.findall(‘owl:Class‘, namespaces)
Prefixes are only looked up in the namespaces
parameter you pass in. This means you can use any namespace prefix you like; the API splits off the owl:
part, looks up the corresponding namespace URL in the namespaces
{http://www.w3.org/2002/07/owl}Class
instead.
If you can switch to the lxml
library things are better; that library supports the same ElementTree API, but collects namespaces for you in a .nsmap
attribute on elements.
於是果斷去裝lxml
服務器上已經裝了python2.7,然而除此之外的東西全都沒有,因此又手動裝了pip
執行pip install lxml 報了一大堆錯,註意到:
Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
查到lxml還需要先安裝依賴庫libxml2和libxslt的開發版
yum install libxml2-devel
yum install libxslt-devel
安裝好依賴庫後pip還是報錯
這次又認真讀了一遍錯誤信息發現前面居然還有一行:
unable to execute gcc: No such file or directory
好吧,連gcc都沒有安裝,難怪無法編譯
yum install gcc
安裝好以後pip install,居然,還是報錯!
src/lxml/lxml.etree.c:84:20: 致命錯誤:Python.h:沒有那個文件或目錄
再查資料,原來還需要安裝一個python-devel,這是Python的頭文件和靜態庫包
yum install python-devel
安裝完之後再次pip install lxml
終於見到了喜聞樂見的
Successfully installed lxml
至此,linux下的lxml安裝完成
在Linux服務器上安裝lxml