在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()anditerfind()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 areonlylooked up in thenamespacesparameter you pass in. This means you can use any namespace prefix you like; the API splits off theowl:part, looks up the corresponding namespace URL in thenamespacesdictionary, then changes the search to look for the XPath expression{http://www.w3.org/2002/07/owl}Classinstead.
If you can switch to thelxmllibrarythings are better; that library supports the same ElementTree API, but collects namespaces for you in a.nsmapattribute 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音火山版如何添加通讯录好友
- 下一篇: nohup命令