㈠ 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了
具体关闭方法请网络
基本我就踩到了这些坑。