python lxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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[0].tag
'body'
 
  
 
循环访问:
 
>>> for child in dom:
...     print child.tag
...
body
 
  
 
查看节点索引
 
>>>body = dom[0]
 
>>> dom.index(body)
0
 
  
 
字节点获取父节点
 
>>> body.getparent().tag
'html'
 
  
 
访问所有子节点
 
>>> for ele in dom.iter():
...     print ele.tag
...
html
body
div
div
 
  
 
3. 访问节点属性
 
>>> body.get('id')
'1'
 
也可以这样
 
>>> attrs = body.attrib
>>> attrs.get('id')
'1'
 
  
 
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"]')[0].tag
'body'
 
  
 
body下的第1个div
 
>>> dom.xpath('body/div[1]')[0].tag
'div'


posted @ 2014-01-13 22:52  arhatlohan  阅读(193)  评论(0编辑  收藏  举报