forked from wxy1343/img-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
516 lines (485 loc) · 17.4 KB
/
spider.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
from req import *
import random
import re
import time
import os
import sys
from config import *
from bs4 import BeautifulSoup
from threading import Lock
from multiprocessing.dummy import Pool as ThreadPool
requests.packages.urllib3.disable_warnings()
sum = 0
count = 0
url_list = []
url_lists = []
favorites_url_list = []
favorites_index = 0
thread = True
threads_num = 100
retry = True
retry_max = 999999999
retry_num = 0
output = False
error_info = True
pool = ThreadPool(threads_num)
start_time = 0
end_time = 0
time_out_retry_num = 0
time_out = 10
lock = Lock()
def img_download(args):
global time_out_retry_num
global retry_num
name, url = args
if name != '':
path = 'img/' + name + '/'
else:
path = 'img/'
while True:
try:
if os.path.exists(path + url[-10:]) == True:
temp_size = os.path.getsize(path + url[-10:])
elif os.path.exists(path + (url[:-4] + '.png')[-10:]) == True:
temp_size = os.path.getsize(path + (url[:-4] + '.png')[-10:])
else:
temp_size = 0
headers = {'Range': 'bytes=%d-' % temp_size}
r = requests.get(url, stream=True, verify=False, headers=headers, timeout=time_out)
while r.status_code == 503:
r = requests.get(url, stream=True, verify=False, headers=headers, timeout=time_out)
if (r.status_code == 404):
url = url[:-4] + '.png'
r = requests.get(url, stream=True, verify=False, headers=headers, timeout=time_out)
while r.status_code == 503:
r = requests.get(url, stream=True, verify=False, headers=headers, timeout=time_out)
with open(path + url[-10:], "ab") as f:
for chunk in r.iter_content(chunk_size=128):
if chunk:
temp_size += len(chunk)
f.write(chunk)
f.flush()
except requests.exceptions.ReadTimeout:
time_out_retry_num += 1
if error_info:
with lock:
print('\n\033[0;37;41m超时重试%d次。\033[0m' % time_out_retry_num)
except (requests.exceptions.ConnectionError, requests.exceptions.ChunkedEncodingError):
if error_info:
with lock:
print('\n\033[0;37;41m远程主机强迫关闭了一个现有的连接。\033[0m')
if (retry and (retry_num < retry_max)):
retry_num += 1
if error_info:
with lock:
print('第%d次重试' % retry_num)
else:
with lock:
print('\n下载失败')
print('感谢您的使用!')
input('按Enter键退出')
sys.exit()
else:
break
with lock:
progress_bar('下载进度', sum)
def progress_bar(text, sum):
global count
count += 1
done = int(50 * count / sum)
char = '╱╲'
sys.stdout.write("\r\033[0;37;42m %s%s:%d%%|%s%s| %d/%d\033[0m" % (
char[count % 2], text, 100 * count / sum, '█' * done, ' ' * (50 - done), count, sum))
sys.stdout.flush()
def init():
os.system('title img-spider @吾爱破解 wxy1343')
print('欢迎使用!\n前往数据源:https://wallhaven.cc 下载更多精彩图片!')
configdir = 'config.ini'
global thread
global threads_num
global retry
global retry_max
global output
global time_out
global error_info
if not os.path.exists(configdir):
f = open(configdir, 'a')
f.close()
cf = Config(configdir, '配置')
cf.Add('配置', 'thread', str(thread))
cf.Add('配置', 'threads_num', str(threads_num))
cf.Add('配置', 'retry', str(retry))
cf.Add('配置', 'retry_max', str(retry_max))
cf.Add('配置', 'output', str(output))
cf.Add('配置', 'time_out', str(time_out))
cf.Add('配置', 'error_info', str(error_info))
else:
cf = Config(configdir, '配置')
thread = cf.GetBool('配置', 'thread')
threads_num = cf.GetInt('配置', 'threads_num')
retry = cf.GetBool('配置', 'retry')
retry_max = cf.GetInt('配置', 'retry_max')
output = cf.GetBool('配置', 'output')
time_out = cf.GetInt('配置', 'time_out')
error_info = cf.GetBool('配置', 'error_info')
if not thread:
threads_num = 1
def parse(url, 开始页数, 页数):
for i in range(开始页数, 页数):
urls = url + 'page=' + str(i + 1)
print('正在爬取第%d页...' % (i + 1))
req = Req()
response = req.get(urls)
soup = BeautifulSoup(response.text, features='lxml')
for j in soup.find_all('figure'):
urls = 'https://w.wallhaven.cc/full/' + j['data-wallpaper-id'][:2] + '/wallhaven-' + j[
'data-wallpaper-id'] + '.jpg'
if urls not in url_list:
url_list.append(urls)
print('第%d页爬取成功' % (i + 1))
def parse_mul(url):
global retry_num
global time_out_retry_num
req = Req()
while True:
try:
response = req.get(url, timeout=time_out)
except requests.exceptions.ReadTimeout:
time_out_retry_num += 1
if error_info:
with lock:
print('\n\033[0;37;41m超时重试%d次。\033[0m' % time_out_retry_num)
except (requests.exceptions.ConnectionError, requests.exceptions.ChunkedEncodingError):
if error_info:
with lock:
print('\n\033[0;37;41m远程主机强迫关闭了一个现有的连接。\033[0m')
if (retry and (retry_num < retry_max)):
retry_num += 1
if error_info:
with lock:
print('第%d次重试' % retry_num)
else:
with lock:
print('\n爬取失败')
print('感谢您的使用!')
input('按Enter键退出')
sys.exit()
else:
if response.status_code != 200:
parse_mul(url)
return
soup = BeautifulSoup(response.text, features='lxml')
for j in soup.find_all('figure'):
urls = 'https://w.wallhaven.cc/full/' + j['data-wallpaper-id'][:2] + '/wallhaven-' + j[
'data-wallpaper-id'] + '.jpg'
if urls not in url_list:
url_list.append(urls)
progress_bar('爬取进度', sum)
break
def parse_favorites(url):
req = Req()
response = req.get(url)
soup = BeautifulSoup(response.text, features='lxml')
favorites_url_list = []
for i, j in enumerate(soup.find('ul', id='collections').find_all('li', {'class': 'collection'})):
name = j.find('span', {'class': 'collection-label'}).text
print(str(i + 1) + '.' + name)
url = j.find('a')['href'] + '?'
favorites_url_list.append((name, url))
return favorites_url_list
def get_page():
start_page = 0
while True:
page = input('请输入爬取页数:').strip()
if re.match('^\d+$', page) != None:
break
elif page == '':
page = 1
break
elif re.match('^\d+(\s+|,+|-+)\d+$', page) != None:
if (',' in page):
start_page = page.split(',')[0]
page = page.split(',')[-1]
if (' ' in page):
start_page = page.split()[0]
page = page.split()[-1]
if ('-' in page):
start_page = page.split('-')[0]
page = page.split('-')[-1]
if (int(start_page) > int(page)) or (int(start_page) <= 0):
print('\033[0;37;41m你的输入非法,请重新输入!\033[0m')
continue
break
else:
print('\033[0;37;41m你的输入非法,请重新输入!\033[0m')
if start_page != 0:
start_page = int(start_page) - 1
page = int(page)
return start_page, page
def Retry(f):
global count
global retry_num
while True:
try:
f()
except (requests.exceptions.ConnectionError, requests.exceptions.ChunkedEncodingError):
if error_info:
with lock:
print('\n\033[0;37;41m远程主机强迫关闭了一个现有的连接。\033[0m')
if (retry and (retry_num < retry_max)):
retry_num += 1
if error_info:
with lock:
print('第%d次重试' % retry_num)
count = 0
else:
with lock:
print('\n下载失败')
print('感谢您的使用!')
input('按Enter键退出')
sys.exit()
else:
break
def favorites(name, url):
global url_list
global url_lists
global favorites_url_list
global favorites_index
global sum
global count
global retry_num
global time_out_retry_num
start_page, page = get_page()
url_lists = []
for i in range(start_page, page):
url_lists.append(url + 'page=' + str(i + 1))
sum = page - start_page
print('开始爬取...')
start_time = time.time()
count = 0
retry_num = 0
if thread:
pool.map(parse_mul, url_lists)
else:
parse(url, start_page, page)
url_lists = [(name, url) for url in url_list]
sum = len(url_list)
if (sum == 0):
print('\033[0;37;41m"%s"为空,正在爬取下一个!\033[0m' % name)
return
print('\n爬取完毕')
if output:
to_txt(url_list)
return
if not os.path.exists('img/' + name + '/'):
os.mkdir('img/' + name + '/')
end_time = time.time()
print('\033[0;37;42m耗时%f秒\033[0m' % (end_time - start_time))
print('开始下载')
start_time = time.time()
count = 0
retry_num = 0
time_out_retry_num = 0
pool.map(img_download, url_lists)
print('\n下载完成')
end_time = time.time()
print('\033[0;37;42m耗时%f秒\033[0m' % (end_time - start_time))
def read_txt(path):
list = []
with open(path, 'r') as f:
for i in f.readlines():
if i != '':
list.append(i.strip().replace('\n', ''))
return list
def to_txt(list):
txt = ''
for i in list:
txt += i + '\n'
txt = txt[:-1]
if not os.path.exists('output'):
os.mkdir('output')
with open('output\\' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.txt', 'w') as f:
f.write(txt)
def txt_spider():
global sum
global count
global retry_num
global time_out_retry_num
global favorites_url_list
global url_list
path = input('请输入文本路径:')
name = path.split('\\')[-1].split('.')[0]
l = read_txt(path)
for url in l:
url_list=[]
count=0
print('正在爬取:' + url)
if re.match('^(https://|http://|)wallhaven.cc($|/.*\?|/.*$|/)', url) != None:
if '&page=' in url:
url = url.split('&page=')[0] + '&'
elif '?page=' in url:
url = url.split('?page=')[0] + '?'
elif re.match('.*favorites($|/$)', url):
start_time = time.time()
print('正在爬取收藏中...')
favorites_url_list = parse_favorites(url.rstrip())
end_time = time.time()
print('\033[0;37;42m耗时%f秒\033[0m' % (end_time - start_time))
elif url.rstrip()[-1] == '/':
url = url[:-1] + '?'
else:
url = url.rstrip() + '?'
spider(url, name, False)
with lock:
print('感谢您的使用!')
input('按Enter键退出')
def main():
# https://wallhaven.cc/search?q=搜索关键词&page=页数
# 获取figure标签的data-wallpaper-id属性值
# https://w.wallhaven.cc/full/(data-wallpaper-id前两位)/wallhaven-(data-wallpaper-id).jpg
init()
global sum
global pool
global start_time
global end_time
global url_list
global favorites_url_list
global favorites_index
global count
global retry_num
global time_out_retry_num
pool = ThreadPool(threads_num)
name = ''
while True:
print('1.搜索关键词\n2.最新\n3.排行榜\n4.随机\n5.自定义\n6.文本')
choice = input('请输入序号:')
if choice == '1':
while True:
keyword = input('请输入搜索关键词:').strip()
if keyword != '':
break
else:
print('\033[0;37;41m关键词不能为空!\033[0m')
url = 'https://wallhaven.cc/search?q=' + keyword + '&'
name = keyword
break
elif choice == '2':
name = 'latest'
url = 'https://wallhaven.cc/latest?'
break
elif choice == '3':
name = 'toplist'
url = 'https://wallhaven.cc/toplist?'
break
elif choice == '4':
seed = input('请输入种子(留空随机种子):')
if (seed.strip() == ''):
seed = ''.join(random.sample([chr(i) for i in range(65, 91)] + [chr(i) for i in range(97, 123)], 5))
print('你的随机种子是:' + seed)
name = seed
url = 'https://wallhaven.cc/random?seed=' + seed + '&'
break
elif choice == '5':
while True:
with lock:
url = input('请输入自定义网址:').strip()
if re.match('^(https://|http://|)wallhaven.cc($|/.*\?|/.*$|/)', url) != None:
if '&page=' in url:
url = url.split('&page=')[0] + '&'
elif '?page=' in url:
url = url.split('?page=')[0] + '?'
elif re.match('.*favorites($|/$)', url):
start_time = time.time()
print('正在爬取收藏中...')
favorites_url_list = parse_favorites(url.rstrip())
end_time = time.time()
print('\033[0;37;42m耗时%f秒\033[0m' % (end_time - start_time))
elif url.rstrip()[-1] == '/':
url = url[:-1] + '?'
else:
url = url.rstrip() + '?'
break
print('\033[0;37;41m你输入的网址有误,请重新输入!\033[0m')
break
elif choice == '6':
txt_spider()
return
else:
print('\033[0;37;41m请输入正确的序号!\033[0m')
continue
print(url)
spider(url, name)
def spider(url='', name='', flag=True):
global favorites_url_list
global count
global url_list
global sum
global retry_num
global time_out_retry_num
name = name.replace('\\', '').replace('/', '').replace(':', '').replace('*', '').replace('?', '').replace('"',
'').replace(
'<', '').replace('>', '').replace('|', '')
start_page, page = get_page()
if not os.path.exists('img'):
os.mkdir('img')
if (favorites_url_list != []):
favorites_url_list = favorites_url_list[start_page:page]
favorites_index = start_page
for i in favorites_url_list:
name, url = i
print('正在爬取第' + str(favorites_index + 1) + '个:' + name)
if (favorites_index < page + 1):
favorites(name, url)
url_list = []
count = 0
favorites_index += 1
else:
url_lists = []
for i in range(start_page, page):
url_lists.append(url + 'page=' + str(i + 1))
sum = page - start_page
with lock:
print('开始爬取...')
start_time = time.time()
count = 0
retry_num = 0
if thread:
pool.map(parse_mul, url_lists)
else:
parse(url, start_page, page)
sum = len(url_list)
if sum == 0 and flag:
with lock:
print('\033[0;37;41m没有匹配的结果,换个关键词试试吧!\033[0m')
main()
end_time = time.time()
with lock:
print('\n爬取完毕')
print('\033[0;37;42m耗时%f秒\033[0m' % (end_time - start_time))
if output:
to_txt(url_list)
if flag:
with lock:
print('感谢您的使用!')
input('按Enter键退出')
sys.exit()
url_lists = [(name, url) for url in url_list]
with lock:
print('开始下载')
if not os.path.exists('img/' + name + '/'):
os.mkdir('img/' + name + '/')
start_time = time.time()
count = 0
retry_num = 0
time_out_retry_num = 0
pool.map(img_download, url_lists)
with lock:
print('\n下载完成')
end_time = time.time()
print('\033[0;37;42m耗时%f秒\033[0m' % (end_time - start_time))
if flag:
with lock:
print('感谢您的使用!')
input('按Enter键退出')
if __name__ == '__main__':
main()