1. python中lxml模块怎么导入
这个模块是第三方模块,需要先安装再导入。
安装:终端命令界面下,pip install lxml(安装过程中如果提示需要其他哪个库,需要先装提示的库,再装lxml)。
如果使用pip安装失败,到pypi社区官网下载压缩包解压,终端界面进入其目录(当前目录有个叫“setup.py”就对了),用命令 python setup install 就行。
导入:import lxml 即可
2. 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 就可以装上了!
3. 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')]
4. python3中 lxml.html 模块怎么用
lxml 模块不是内置的,需要先安装才能使用。 lxml安装依赖 python-devel,libxml2-devel,libxslt-devel, 装好之后,下载 http://codespeak.net/lxml/lxml-2.2.8.tgz, tar zxvf lxml-2.2.8.tgz, 然后python setup.py install即可
5. 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
6. python爬虫lxml基本用法
python3环境下安装命令
用lxml解析html,利用etree.HTML解析字符串将字符串解析从html格式的文件, 经过处理后,部分缺失的节点可以自动修复,并且还自动添加了 body、html 节点
通过 / 或 // 即可查找元素的子节点或子孙节点。
选择 li 节点的所有直接 a 子节点xpath为://li/a
标签[@属性=“”]
@text()
/@属性
[contains(@属性,"值")]