jxdiscuz 发表于 2017-5-2 10:50:47

python html parser库lxml的介绍和使用

使用由 Python 编写的 lxml 实现高性能 XML 解析 http://blog.csdn.net/yatere/article/details/6667043
用lxml解析HTMLhttp://www.cnblogs.com/descusr/archive/2012/06/20/2557075.html

分步遍历:比先遍历得到body的某个div,然后在使用这个div的内容做遍历基础,继续通过它往下遍历
def scanningHotArticle(url):
print url
request=requests.get(url)
dom=soupparser.fromstring(request.content)
body=dom
articleList=body.xpath("//div[@class='block untagged mb15 bs2']")
for article in articleList:
articleStr= etree.tostring(article)
articleBody=soupparser.fromstring(articleStr)
print len(articleBody.xpath("//div[@class='detail']"))
结构是body-*->div,这个div下面又存在许多div,然后把这个div当作根节点,在继续xpath查找下面的元素。

python html parser库lxml的介绍和使用(快速入门)
http://blog.csdn.net/marising/article/details/5821090
lxm是python的一个html/xml解析并建立dom的库,lxml的特点是功能强大,性能也不错,xml包含了ElementTree ,html5lib ,beautfulsoup 等库,但是lxml也有自己相对应的库,所以,导致lxml比较复杂,初次使用者很难了解其关系。
1.解析html并建立dom
>>> import lxml.etree as etree
>>> html = '<html><body id="1">abc<div>123</div>def<div>456</div>ghi</body></html>'
>>> dom = etree.fromstring(html)
>>> etree.tostring(dom)
'<html><body id="1">abc<div>123</div>def<div>456</div>ghi</body></html>'

如果用beautifulsoup的解析器,则
>>> import lxml.html.soupparser as soupparser
>>> dom = soupparser.fromstring(html)
>>> etree.tostring(dom)
'<html><body id="1">abc<div>123</div>def<div>456</div>ghi</body></html>'
但是我强烈建议使用soupparser,因为其处理不规范的html的能力比etree强太多。

2.按照Dom访问Element
子元素长度
>>> len(dom)
1

访问子元素:
>>> dom.tag
'body'

循环访问:
>>> for child in dom:
...   print child.tag
...
body

查看节点索引
>>>body = dom
>>> dom.index(body)
0

字节点获取父节点
>>> body.getparent().tag
'html'

访问所有子节点
>>> for ele in dom.iter():
...   print ele.tag
...
html
body
div
div
遍历和打印所有子节点:
>>> children = list(root)
>>> for child in root:
... print(child.tag)

元素的兄弟或邻居节点是通过next和previous属性来访问的
The siblings (or neighbours) of an element are accessed as next and previous elements:
>>> root is root.getprevious() # lxml.etree only!
True
>>> root is root.getnext() # lxml.etree only!
True

3. 访问节点属性
>>> body.get('id')
'1'
也可以这样
>>> attrs = body.attrib
>>> attrs.get('id')
'1'

带属性的元素
XML元素支持属性,可以用Element工厂方法直接创建。
>>> root = etree.Element("root", interesting="totally")
>>> etree.tostring(root)
b’<root interesting="totally"/>’

可以使用set和get方法访问这些属性:
>>> print root.get("interesting")
totally
>>> root.set("interesting", "somewhat")
>>> print root.get("interesting")
somewhat

也可以使用attrib性质的字典接口
>>> attributes = root.attrib
>>> print(attributes["interesting"])
somewhat
>>> print(attributes.get("hello"))
None
>>> attributes["hello"] = "Guten Tag"
>>> print(attributes.get("hello"))
Guten Tag
>>> print(root.get("hello"))
Guten Tag

4. 访问Element的内容
>>> body.text
'abc'
>>> body.tail
text只是从本节点开始到第一个字节点结束;tail是从最后一个字节结束到本节点未知。
访问本节点所有文本信息
>>> body.xpath('text()')
['abc', 'def', 'ghi']

访问本节点和子节点所有文本信息
>>> body.xpath('//text()')
['abc', '123', 'def', '456', 'ghi']

貌似返回本文档中所有文字信息
body.text_content()返回本节点所有文本信息。
5.Xpath的支持
所有的div元素
>>> for ele in dom.xpath('//div'):
...   print ele.tag
...
div
div

id=“1”的元素
>>> dom.xpath('//*[@id="1"]').tag
'body'

body下的第1个div
>>> dom.xpath('body/div').tag
'div'

参考:
lxml的官方文档:http://codespeak.net/lxml/
HtmlParser的性能:http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/
页: [1]
查看完整版本: python html parser库lxml的介绍和使用