forked from jojoin/cutout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
233 lines (129 loc) · 4.3 KB
/
example.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os, time
from cutout import cutout
datastr = '''
<html>
<head>
<title>html网页标题</title>
</head>
<body>
<ul id="img">
<li> <p>pic1</p> <img src="/img/pic1.jpg" /> </li>
<li> <p>pic2</p> <img src="/img/pic2.jpg" /> </li>
<li> <p>pic3</p> <img src="/img/pic3.jpg" /> </li>
</ul>
</body>
</html>
'''
# 获取网页title
title = cutout(
data=datastr,
start="<title>",
end="</title>"
)
print(title) # html网页标题
# 获取图片地址
href = cutout(
data=datastr,
start="<ul id=\"img\">",
end="</ul>",
split="<li>", #分割
dealwith=({
"start":"<p>", #获取名称
"end":"</p>"
},{
"start":'<img src="', #获取网址
"rid":'"',
"end":'"'
})
)
print(href) # [['', None], ['pic1', '/img/pic1.jpg'], ['pic2', '/img/pic2.jpg'], ['pic3', '/img/pic3.jpg']]
# 获取的结果数组第一个为 ['', None] 因为以 <li> 分割时 第一段字符为空
exit(0);
from cutout.cache import FileCache
print('\n\n######## cache缓存测试\n')
print("\n## FileCache 文件缓存测试\n")
key = '缓存键 hash key'
c = FileCache('./cache') #指定缓存目录
c.set(key, ['2w3w','agafd'],10)
g = c.get(key)
print('取回设置的缓存值:'+g[1])
import cutout.util as util
print('\n\n######## util工具类测试')
print('\n## rangable 限定数值范围\n')
print('128 (50-100) => '+str(util.rangable(128,50,100)))
print('32.5 (50.4-100) => '+str(util.rangable(32.5,50.4,100)))
print('32 (50.4-无上限) => '+str(util.rangable(32.5,50.4)))
print('128 (无下限-100) => '+str(util.rangable(128,top=100)))
print('\n## parse_argv 解析命令行参数\n')
print(
"['test.py','-k','key','-n','num'] => "
+str(util.parse_argv(['test.py','-k','key','-n','num']))
)
print('\n## sec2time time2sec 时间格式转化\n')
print('1324 => '+util.sec2time(1324))
print('22:04 => '+str(util.time2sec('22:04')))
print('\n## urlencode urldncode url编码解码\n')
print('中文汉字 => '+util.urlencode('中文汉字'))
print(
'%E4%B8%AD%E6%96%87%E6%B1%89%E5%AD%97 => '
+util.urldecode('%E4%B8%AD%E6%96%87%E6%B1%89%E5%AD%97')
)
print("{'name':'汉字','title':'标题'} => "
+util.urlencode({'name':'汉字','title':'标题'}))
from cutout import cutout, cutouts, download
from cutout.common import ProgressBar
print('\n\n######## cutout抓取函数')
print('\n## cutout 抓取百度音乐PC版软件下载地址 http://music.baidu.com\n')
exe_href = cutout(
url='http://music.baidu.com', #第一步抓取指定内容
start='<a class="downloadlink-pc"',
end='>下载PC版</a>',
dealwith={
'start':'href="', #第二部抓取 href 链接
'rid':'"',
'end':'"'
}
)
print(exe_href)
print('\n## url_download 下载 '+exe_href+' 显示下载进度条\n')
#自定义下载进度条
bar = ProgressBar(piece_total=1);
bar.face(
sh_piece_division=1024, #piece 除法
sh_piece_unit='KB' #piece 单位
)
download(exe_href,showBar=bar)
print('\n\n######## 多线程数据抓取\n')
urls = []
for i in range(18):
'''
urls.append('http://www.tvmao.com/query.jsp?keys=%E6%B9%96%E5%8C%97%E4%BD%93%E8%82%B2')
urls.append('http://jojoin.com/')
urls.append('http://codekart.jojoin.com/')
urls.append('http://docs.codekart.jojoin.com/')
urls.append('http://v.duole.com/')
urls.append('http://www.taobao.com/')
urls.append('http://www.baidu.com/')
urls.append('http://blog.csdn.net/vah101/article/details/6175406')
urls.append('http://www.cnblogs.com/wxw0813/archive/2012/09/18/2690694.html')
urls.append('http://woodpecker.org.cn/diveintopython3/')
urls.append('http://www.pythonclub.org/python-basic/threading')
urls.append('http://developer.51cto.com/art/201003/185569.htm')
'''
urls.append('http://v.baidu.com/tv/21331.htm')
#urls.append('')
sti = time.time()
bdata = cutouts(urls=urls)
eti = time.time()
#print(bdata)
print('并发抓取%d个页面:'%len(urls)+'%.2f'%(eti-sti)+'秒\n')
bdata = []
sti = time.time()
for u in urls:
bdata.append(cutout(u))
eti = time.time()
#print(bdata)
print('顺序抓取%d个页面:'%len(urls)+'%.2f'%(eti-sti)+'秒\n')
print('\n\n#### cutout已完成所有测试!!!')