forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_xpath.py
More file actions
63 lines (52 loc) · 1.37 KB
/
test_xpath.py
File metadata and controls
63 lines (52 loc) · 1.37 KB
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
# encoding: utf-8
__author__ = 'zhanghe'
def test_doc_xpath():
"""
测试document的xpath用法
:return:
"""
import lxml.html
html = u'''
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="10%" nowrap="" style="padding-bottom: 5px;">数量: 1</td>
<td width="90%" align="right" nowrap="" style="padding-right: 5px;"></td>
</tr>
</tbody>
</table>
'''
doc = lxml.html.fromstring(html)
num_list = doc.xpath('//td[@style="padding-bottom: 5px;" and @nowrap="" and not(@align="right")]/text()')
print type(num_list[0])
print num_list[0]
def test_etree_xpath():
"""
测试etree的xpath用法
:return:
"""
from lxml import etree
html = u'''
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="10%" nowrap="" style="padding-bottom: 5px;">数量: 1</td>
<td width="90%" align="right" nowrap="" style="padding-right: 5px;"></td>
</tr>
</tbody>
</table>
'''
tree = etree.HTML(html)
num_list = tree.xpath('//td[@style="padding-bottom: 5px;" and @nowrap="" and not(@align="right")]/text()')
print type(num_list[0])
print num_list[0]
if __name__ == '__main__':
test_doc_xpath()
test_etree_xpath()
"""
测试结果:
<type 'lxml.etree._ElementUnicodeResult'>
数量: 1
<type 'lxml.etree._ElementUnicodeResult'>
数量: 1
"""