-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path22-PyQuery.py
148 lines (109 loc) · 4.83 KB
/
22-PyQuery.py
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
143
144
145
146
147
148
'''
Python中PyQuery库的使用总结
pyquery库是jQuery的Python实现,可以用于解析HTML网页内容
'''
from pyquery import PyQuery as pq
print('----1.可加载一段HTML字符串,或一个HTML文件,或是一个url地址----')
d1 = pq('<html><title>hello</title></html>')
d2 = pq(url='http://movie.douban.com/subject/1309069/')
#d3 = pq(filename='result\\02-tieba\\16pn=1.html')
print('----2.html()和text() ——获取相应的HTML块或文本块----')
d1 = pq('<html><title>hello</title></html>')
print(d1('html')) # <html><title>hello</title></html>
print(d1('html').html()) # <title>hello</title>
print(d1('html').text()) # hello
print('----3.根据HTML标签来获取元素----')
d=pq('<div><p>test 1</p><p>test 2</p></div>')
print(d('p')) # <p>test 1</p><p>test 2</p>
print(d('p').html()) # test 1
print('----4.eq(index) ——根据给定的索引号得到指定元素----')
d=pq('<div><p>test 1</p><p>test 2</p></div>')
print(d('p').html())
print(d('p').eq(0).html()) # test 1
print(d('p').eq(1).html()) # test 2
print(d('p').eq(2).html()) # None
print('----5.filter() ——根据类名、id名得到指定元素----')
d=pq("<div><p id='one'>test 1</p><p class='two'>test 2</p></div>")
print(d('p').filter('#one')) # <p id="one">test 1</p>
print(d('p').filter('.two')) # <p class="two">test 2</p>
print('----6.find() ——查找嵌套元素----')
d=pq("<div><p id='one'>test 1</p><p class='two'>test 2</p></div>")
print(d('div').find('p')) # <p id="one">test 1</p><p class="two">test 2</p>
print(d('div').find('p').eq(0)) # <p id="one">test 1</p>
print('----7.直接根据类名、id名获取元素----')
d=pq("<div><p id='one'>test 1</p><p class='two'>test 2</p></div>")
print(d('#one').html()) # test 1
print(d('.two').html()) # test 2
print('----8.获取属性值----')
d=pq("<p id='my_id'><a href='http://hello.com'>hello</a></p>")
print(d('a').attr('href')) # http://hello.com
print(d('p').attr('id')) # my_id
print('----9.修改属性值----')
d=pq("<p id='my_id'><a href='http://hello.com'>hello</a></p>")
print(d) # <p id="my_id"><a href="http://hello.com">hello</a></p>
d('a').attr('href','http://baidu.com')
print(d) # <p id="my_id"><a href="http://baidu.com">hello</a></p>
print('----10.addClass(value) ——为元素添加类----')
d=pq('<div></div>')
print(d) # <div/>
d.addClass('my_class')
print(d) # <div class="my_class"/>
print('----11.hasClass(name) #返回判断元素是否包含给定的类----')
d=pq("<div class='my_class'></div>")
print(d.hasClass('my_class')) # True
print(d.hasClass('my_c')) # False
print('----12.children(selector=None) ——获取子元素----')
d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")
print(d.children()) # <p id="one">hello</p><p id="two">world</p>
print(d.children('#two')) # <p id="two">world</p>
print('----13.parents(selector=None)——获取父元素----')
d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")
print(d('p').parents()) # <span><p id="one">hello</p><p id="two">world</p></span>
print(d('#one').parents('span'))# <span><p id="one">hello</p><p id="two">world</p></span>
print(d('#one').parents('p')) # []
print('----14.clone() ——返回一个节点的拷贝----')
d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")
print(d('#one')) # <p id="one">hello</p>
print(d('#one').clone()) # <p id="one">hello</p>
print('----15.empty() ——移除节点内容----')
d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")
print(d) # <span><p id="one">hello</p><p id="two">world</p></span>
d('#one').empty()
print(d) # <span><p id="one"/><p id="two">world</p></span>
print('----16.nextAll(selector=None) ——返回后面全部的元素块----')
d=pq("<p id='one'>hello</p><p id='two'>world</p><img scr='' />")
print(d('p:first').nextAll()) # <p id="two">world</p><img scr=""/>
print(d('p:last').nextAll()) # <img scr=""/>
print('----17.not_(selector) ——返回不匹配选择器的元素----')
d=pq("<span><p id='one'>hello</p><p id='two'>world</p></span>")
print(d('p').not_('#two')) # <p id="one">hello</p>
'''
爬取豆瓣电影页面中主演
'''
if __name__ == '__main__':
print('----爬取豆瓣电影页面中主演----')
# 读取Batman Begins页面
doc = pq(url='http://movie.douban.com/subject/3077412/')
# 遍历starring节点
starring = doc("a[rel='v:starring']")
# 转化为Map
stars = starring.map(lambda i,e:pq(e).text())
print('<<%s>>的主演:' % (doc("span[property='v:itemreviewed']").text()))
for i in stars:
print(i)
'''
执行结果:
----爬取豆瓣电影页面中主演----
<<寻龙诀>>的主演:
陈坤
黄渤
舒淇
杨颖
夏雨
刘晓庆
颜卓灵
曹操
张东
黄西
僧格仁钦
'''