-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdown.py
More file actions
526 lines (518 loc) · 31 KB
/
pdown.py
File metadata and controls
526 lines (518 loc) · 31 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
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
517
518
519
520
521
522
523
524
525
526
# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse
import re
import time
import os
import requests
from bs4 import BeautifulSoup
def judge_user():#获取cookie并判断
global user_name
global cookies
cookies=input("请输入cookie信息,如不输入可以直接跳过,但是无法获取收藏夹及标签查找:")
url="https://www.pixiv.net/"
headers={"Cookie":cookies}
html=requests.get(url,headers=headers).text
req=r'"href="/member.php.id=(\d*)"style="background-image'
req2=r'</a><div class="user-name-container"><a class="user-name js-click-trackable-later"href="/member.php.id=\d*"data-click-category="mypage-profile-column-simple"data-click-action="click-profile"data-click-label="">(.*?)</a></div>'
user_id=re.findall(req,html)
if user_id:
user_name=re.findall(req2,html)[0]
print("欢迎用户--%s--"%user_name)
else:
print("cookie有误,将使用默认账号登录")
user_name="默认账号"
f=open("cookie.txt","r")
cookies=f.readline()
f.close()
global url_root,judge_number
url_root="https://www.pixiv.net/bookmark.php?rest=show&p="
judge_number=0
def book_mark():
def find_page():#查找收藏夹的作品数
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Connection":"keep-alive",
"Cookie":cookies}
url=url_root+str(1)
html=requests.get(url,headers=headers).text
reg=r'<span class="count-badge">(\d*)件</span></div>'
page=re.findall(reg,html)
print("--------------------------------------------------共找到%s个作品--------------------------------------------------"%page[0])
return page[0]
def find_rank(page):#查找图片的id,返回一个列表
url=url_root+str(page)
req=urllib.request.Request(url)
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
html=urllib.request.urlopen(req).read().decode('utf-8')
reg = r'<a href="member_illust.php.mode=medium&illust_id=(\d*)"><h1 class="title'#正则表达式查找到id所在的网址
list_id=re.findall(reg,html)
return list_id
def judge_like(img_id,txt_name):#判断图片的点赞数,本来是打算收藏数的,但是没有会员(
print("正在判断%s"%img_id,end="\t")
try:
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(img_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'class="views">(\d*)</span></li></ul>'#正则表达式查看赞数
list_like=re.findall(reg2,html)
text_name=str(time.strftime('%Y_%m_%d',time.localtime(time.time())))+'.txt'
print("喜欢程度:%s"%list_like[0])
with open('picture/'+txt_name+'/rank_list/'+ text_name, 'a')as f:
f.write('https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+img_id+'\n'+'like:'+list_like[0]+"\n")
if int(list_like[0])>=judge_number:#进行参数判断
with open('picture/'+txt_name+'/rank_list/'+"喜爱"+str(judge_number)+"#"+text_name, 'a')as f:
f.write('https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+img_id+'\n'+'like:'+list_like[0]+"\n")
return 1
except:
return 0
def find_img(rank_id):#通过图片所在页面查找图片具体位置
try:
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(rank_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'<img src="https://i.pximg.net/c/600x600/(.*?)" alt='#正则表达式查看图片具体位置
list_img=re.findall(reg2,html)
if len(list_img)>0:#代码测试发现存在无法找到图片的情况,经过验证应该是因为投稿的是动图,实现每次意外会将自动下载一张⑨!
img_place=list_img[0]
return('https://i.pximg.net/'+img_place)
else:
print('恭喜你,多了个⑨',end='\t')
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
except:
print("似乎作者删除了这个图片,恭喜你又多个⑨",end='\t')#作者删除图片的话会404,无权限也会(
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
def down_img(img_place,img_id,count,txt_name):#下载图片
url=str(img_place)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
img=urllib.request.urlopen(req).read()
with open('picture/'+txt_name+r'/'+img_id+'.jpg','wb')as f:
print('正在下载图片%9s' %img_id,end='\t')
f.write(img)
print('下载完成')
count=count+1
return count
count=0
start_time=time.time()
stt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start_time))
txt_name=user_name+"/"+"收藏"
#新建文件夹用于保存每日的图片
if int(os.path.exists('picture/'+txt_name))==0:
print('正在创建目录%s' %txt_name,end='\t')
os.makedirs('picture/'+txt_name)
print('创建完成')
if int(os.path.exists('picture/'+txt_name+'/rank_list'))==0:
os.makedirs('picture/'+txt_name+'/rank_list')
global skip_number
skip_number=1
pages=int(find_page())//20+2
if skip_number:
for page in range(1,pages):
if skip_number:
print("------------------正在下载第%d页,共有%d页------------------" %(page,pages-1))
for i in find_rank(page):
if skip_number:
file_name='picture/'+txt_name+r'/'+i+'.jpg'#以id保存图片
if os.path.exists(file_name):
log_path="picture/"+txt_name+"/"+"log.txt"
if os.path.exists(log_path):
skip_number=0
print("已完成对该收藏夹的下载")
else:
print('id%9s图片已存在,正在跳过' %i) #检查图片是否已存在,防止重复下载
else:
if skip_number:
if judge_like(i,txt_name):
if skip_number:
count=down_img(find_img(i),i,count,txt_name)
end_time=time.time()
edt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(end_time))
print('已完成全部下载,谢谢使用,本次下载%3d图片' %count)
print('开始下载的时间是%s\n结束下载的时间是%s' %(stt,edt))
process=(end_time-start_time)/60
print('本次下载耗时%.2f分钟' %process)
with open('picture/'+txt_name+'/''log.txt','a')as f:
f.write('已完成全部下载,谢谢使用,本次下载%3d图片\n开始下载的时间是%s\n结束下载的时间是%s\n下载耗时%.2f分钟\n' %(count,stt,edt,process))#生成日志文件保存下载信息
os.system('picture/'+txt_name+'/''log.txt')
#不足:漫画只可以保存第一张,虽然将动图替换保存为了琪露诺,但是无法获取动图,其次对于png格式图片无法获取到真是原图。
def p_tag():#获取用户指定的tag图片的函数
global url_root,judge_number
search_word=input("请输入搜索tag:")
words=urllib.parse.quote(search_word)
url_root="https://www.pixiv.net/search.php?word="+words+"&order=date_d&p="
judge_number=int(input("请输入判断数:(必须为整型,否则会崩溃)"))
def find_page():#查找页码
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Connection":"keep-alive",
"Cookie":cookies}
url=url_root+str(1)
html=requests.get(url,headers=headers).text
reg=r'(\d*)件投稿'
page=re.findall(reg,html)
print("--------------------------------------------------共找到%s个作品--------------------------------------------------"%page[0])
return page[0]
def find_rank(page):#获取图片的id
url=url_root+str(page)
req=urllib.request.Request(url)
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
html=urllib.request.urlopen(req).read().decode('utf-8')
reg =r'{"illustId":"(\d*)","illustTitle'#正则表达式查找到id所在的网址
list_id=re.findall(reg,html)
time.sleep(0.5)
return list_id
def judge_like(img_id,txt_name):#判断赞数
print("正在判断%s"%img_id,end="\t")
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(img_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'class="views">(\d*)</span></li></ul>'#正则表达式查看赞数
list_like=re.findall(reg2,html)
text_name=str(time.strftime('%Y_%m_%d',time.localtime(time.time())))+'.txt'
print("喜欢程度:%s"%list_like[0])
with open('picture/'+txt_name+'/rank_list/'+ text_name, 'a')as f:
f.write('https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+img_id+'\n'+'like:'+list_like[0]+"\n")
if int(list_like[0])>=judge_number:
return 1
def find_img(rank_id):
try:
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(rank_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'<img src="https://i.pximg.net/c/600x600/(.*?)" alt='#正则表达式查看图片具体位置
list_img=re.findall(reg2,html)
if len(list_img)>0:#代码测试发现存在无法找到图片的情况,经过验证应该是因为投稿的是动图,实现每次意外会将自动下载一张⑨!
img_place=list_img[0]
return('https://i.pximg.net/'+img_place)
else:
print('恭喜你,多了个⑨',end='\t')
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
except:
print("似乎作者删除了这个图片,恭喜你又多个⑨",end='\t')#作者删除图片的话会404
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
def down_img(img_place,img_id,count,txt_name):
url=str(img_place)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
img=urllib.request.urlopen(req).read()
with open('picture/'+txt_name+r'/'+img_id+'.jpg','wb')as f:
print('正在下载图片%9s' %img_id,end='\t')
f.write(img)
print('下载完成')
count=count+1
time.sleep(0.5)
return count
count=0
start_time=time.time()
stt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start_time))
txt_name=input("输入文件名(默认为D盘picture下的目录)")
txt_name=user_name+"/"+txt_name
#新建文件夹用于保存每日的图片
if int(os.path.exists('picture/'+txt_name))==0:
print('正在创建目录%s' %txt_name,end='\t')
os.makedirs('picture/'+txt_name)
print('创建完成')
if int(os.path.exists('picture/'+txt_name+'/rank_list'))==0:
os.makedirs('picture/'+txt_name+'/rank_list')
pages=int(find_page())//40+2
for page in range(1,pages):
print("------------------正在下载第%d页,共有%d页------------------" %(page,pages-1))
for i in find_rank(page):
file_name='picture/'+txt_name+r'/'+i+'.jpg'#以id保存图片
if os.path.exists(file_name):
print('id%9s图片已存在,正在跳过' %i) #检查图片是否已存在,防止重复下载
else:
if judge_like(i,txt_name):
count=down_img(find_img(i),i,count,txt_name)
end_time=time.time()
edt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(end_time))
print('已完成全部下载,谢谢使用,本次下载%3d图片' %count)
print('开始下载的时间是%s\n结束下载的时间是%s' %(stt,edt))
process=(end_time-start_time)/60
print('本次下载耗时%.2f分钟' %process)
with open('picture/'+txt_name+'/''log.txt','a')as f:
f.write('已完成全部下载,谢谢使用,本次下载%3d图片\n开始下载的时间是%s\n结束下载的时间是%s\n下载耗时%.2f分钟\n' %(count,stt,edt,process))#生成日志文件保存下载信息
os.system('picture/'+txt_name+'/''log.txt')
def get_rank():#获取排行榜的函数1
def find_rank(page):#通过每日排行榜获取最新没张图的链接,保存为一个列表
url="https://www.pixiv.net/ranking_area.php?type=detail&no="+str(page)
req=urllib.request.Request(url)
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
html=urllib.request.urlopen(req).read().decode('utf-8')
reg = r'<h2><a href="(.*?)">'#正则表达式查找到id所在的网址
list_img=re.findall(reg,html)
rank_place=['北海道_东北','关东','中部','近畿','中国_四国','九州_冲绳','国际']
text_name=str(rank_place[page])+'.txt'
#新建一个文本文档用于存储id信息,文件名是程序运行的时间,加上对应排行榜的标题
if os.path.exists('picture/'+txt_name+'/rank_list/'+text_name):
os.remove('picture/'+txt_name+'/rank_list/'+text_name)#先移除旧文档,再新建,防止信息重复
for i in list_img:
with open('picture/'+txt_name+'/rank_list/'+text_name, 'a')as f:
f.write('https://www.pixiv.net/'+i+'\n')
rank_id=[]#创建一个空列表用于储存id
for i in list_img:
rank_id.append(i.split('=')[-1])
return rank_id
def find_img(rank_id):
try:
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(rank_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
#添加头文件信息
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'<img src="https://i.pximg.net/c/600x600/(.*?)" alt='#正则表达式查看图片具体位置
list_img=re.findall(reg2,html)
if len(list_img)>0:#代码测试发现存在无法找到图片的情况,经过验证应该是因为投稿的是动图,实现每次意外会将自动下载一张⑨!
img_place=list_img[0]
return('https://i.pximg.net/'+img_place)
else:
print('恭喜你,多了个⑨',end='\t')
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
except:
print("似乎作者删除了这个图片,恭喜你又多个⑨",end='\t')#作者删除图片的话会404
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
def down_img(img_place,img_id,count,txt_name):
url=str(img_place)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
img=urllib.request.urlopen(req).read()
with open('picture/'+txt_name+r'/'+img_id+'.jpg','wb')as f:
print('正在下载图片%9s' %img_id,end='\t')
f.write(img)
print('下载完成')
count=count+1
return count
count=0
start_time=time.time()
stt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start_time))
global txt_name
txt_name=str(time.strftime('%Y_%m_%d',time.localtime(time.time())))
txt_name="rank/" +txt_name
#新建文件夹用于保存每日的图片
if int(os.path.exists('picture/'+txt_name))==0:
print('正在创建目录%s' %txt_name,end='\t')
os.makedirs('picture/'+txt_name)
print('创建完成')
if int(os.path.exists('picture/'+txt_name+'/rank_list'))==0:
os.makedirs('picture/'+txt_name+'/rank_list')
for page in range(7):
for i in find_rank(page):
file_name='picture/'+txt_name+r'/'+i+'.jpg'#以id保存图片
if os.path.exists(file_name):
print('id%9s图片已存在,正在跳过' %i)
else:
count=down_img(find_img(i),i,count,txt_name)
end_time=time.time()
edt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(end_time))
print('已完成全部下载,谢谢使用,本次下载%3d图片' %count)
print('开始下载的时间是%s\n结束下载的时间是%s' %(stt,edt))
process=(end_time-start_time)/60
print('本次下载耗时%.2f分钟' %process)
with open('picture/'+txt_name+'/''log.txt','a')as f:
f.write('已完成全部下载,谢谢使用,本次下载%3d图片\n开始下载的时间是%s\n结束下载的时间是%s\n下载耗时%.2f分钟\n' %(count,stt,edt,process))#生成日志文件保存下载信息
os.system('picture/'+txt_name+'/''log.txt')
#不足:漫画只可以保存第一张,虽然将动图替换保存为了琪露诺,但是无法获取动图,其次对于png格式图片无法获取到真是原图。
def art_collect(art_id):#保存收藏夹函数
global url_root,judge_number
url_root="https://www.pixiv.net/member_illust.php?id="+art_id+"&type=all&p="
judge_number=0
def find_page():#查找收藏夹的作品数
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Connection":"keep-alive",
"Cookie":cookies}
url=url_root+str(1)
html=requests.get(url,headers=headers).text
reg=r'</a></h1><span class="count-badge">(\d*)件</span>'
page=re.findall(reg,html)
print("--------------------------------------------------共找到%s个作品--------------------------------------------------"%page[0])
return page[0]
def find_rank(page):#查找图片的id,返回一个列表
url=url_root+str(page)
req=urllib.request.Request(url)
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
html=urllib.request.urlopen(req).read().decode('utf-8')
reg = r'<li class="image-item"><a href="/member_illust.php.mode=medium&illust_id=(\d*)"class'#正则表达式查找到id所在的网址
list_id=re.findall(reg,html)
return list_id
def judge_like(img_id,txt_name):#判断图片的点赞数,本来是打算收藏数的,但是没有会员(
print("正在判断%s"%img_id,end="\t")
try:
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(img_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'class="views">(\d*)</span></li></ul>'#正则表达式查看赞数
list_like=re.findall(reg2,html)
text_name=str(time.strftime('%Y_%m_%d',time.localtime(time.time())))+'.txt'
print("喜欢程度:%s"%list_like[0])
with open('picture/'+txt_name+'/rank_list/'+ text_name, 'a')as f:
f.write('https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+img_id+'\n'+'like:'+list_like[0]+"\n")
if int(list_like[0])>=judge_number:#进行参数判断
return 1
except:
return 0
def find_img(rank_id):#通过图片所在页面查找图片具体位置
try:
url='https://www.pixiv.net/member_illust.php?mode=medium&illust_id='+str(rank_id)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
html=urllib.request.urlopen(req).read().decode('utf-8')
reg2=r'<img src="https://i.pximg.net/c/600x600/(.*?)" alt='#正则表达式查看图片具体位置
list_img=re.findall(reg2,html)
if len(list_img)>0:#代码测试发现存在无法找到图片的情况,经过验证应该是因为投稿的是动图,实现每次意外会将自动下载一张⑨!
img_place=list_img[0]
return('https://i.pximg.net/'+img_place)
else:
print('恭喜你,多了个⑨',end='\t')
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
except:
print("似乎作者删除了这个图片,恭喜你又多个⑨",end='\t')#作者删除图片的话会404,无权限也会(
return('https://i.pximg.net/img-original/img/2012/07/25/05/39/57/28864480_p0.jpg')
def down_img(img_place,img_id,count,txt_name):#下载图片
url=str(img_place)
req=urllib.request.Request(url)
req.add_header('Referer','https://www.pixiv.net/ranking_area.php?type=detail&no=6')
req.add_header("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
req.add_header("Connection","keep-alive")
req.add_header("Cookie",cookies)
img=urllib.request.urlopen(req).read()
with open('picture/'+txt_name+r'/'+img_id+'.jpg','wb')as f:
print('正在下载图片%9s' %img_id,end='\t')
f.write(img)
print('下载完成')
count=count+1
return count
def find_art_name():
html=requests.get(url_root+str(1))
soup=BeautifulSoup(html.text)
art_name= soup.title.string.split("」")[0].split("「")[1]#获取画师名称
return art_name
count=0
start_time=time.time()
stt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start_time))
txt_name="画师投稿/"+find_art_name()+"("+art_id+")"
try:
if int(os.path.exists('picture/'+txt_name))==0:
print('正在创建目录%s' %txt_name,end='\t')
os.makedirs('picture/'+txt_name)
print('创建完成')
except:
txt_name="画师投稿/"+"id_"+"("+art_id+")"
if int(os.path.exists('picture/'+txt_name))==0:
print('正在创建目录%s' %txt_name,end='\t')
os.makedirs('picture/'+txt_name)
print('创建完成')
if int(os.path.exists('picture/'+txt_name+'/rank_list'))==0:
os.makedirs('picture/'+txt_name+'/rank_list')
global skip_number#判断是否已经下载过该画师图集的标记
skip_number=1
pages=int(find_page())//20+2
if skip_number:
for page in range(1,pages):
if skip_number:
print("------------------正在下载第%d页,共有%d页------------------" %(page,pages-1))
for i in find_rank(page):
file_name='picture/'+txt_name+r'/'+i+'.jpg'#以id保存图片
if os.path.exists(file_name):
log_path="picture/"+txt_name+"/"+"log.txt"
if os.path.exists(log_path):
print("已完成对该画师图片的下载")
skip_number=0
break
else:
print('id%9s图片已存在,正在跳过' %i) #检查图片是否已存在,防止重复下载
else:
if skip_number:
if judge_like(i,txt_name):
if skip_number:
count=down_img(find_img(i),i,count,txt_name)
end_time=time.time()
edt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(end_time))
print('已完成全部下载,谢谢使用,本次下载%3d图片' %count)
print('开始下载的时间是%s\n结束下载的时间是%s' %(stt,edt))
process=(end_time-start_time)/60
print('本次下载耗时%.2f分钟' %process)
with open('picture/'+txt_name+'/''log.txt','a')as f:
f.write('已完成全部下载,谢谢使用,本次下载%3d图片\n开始下载的时间是%s\n结束下载的时间是%s\n下载耗时%.2f分钟\n' %(count,stt,edt,process))#生成日志文件保存下载信息
os.system('picture/'+txt_name+'/''log.txt')
def find_artists():#查找用户关注画师,并下载画师作品
def find_page():
url="https://www.pixiv.net/bookmark.php?type=user&rest=show"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Connection":"keep-alive",
"Cookie":cookies}
html=requests.get(url,headers=headers).text
reg=r'</a></h1><div class="unit-count">(\d*)</div>'
page=re.findall(reg,html)
print("--------------------------------------------------共找到%s个画师--------------------------------------------------"%page[0])
return int(page[0])//48+2
def find_artists_id(page):
artists_id=[]
url="https://www.pixiv.net/bookmark.php?type=user&rest=show&p="+str(page)
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Connection":"keep-alive",
"Cookie":cookies}
html=requests.get(url,headers=headers)
soup=BeautifulSoup(html.text,"lxml")
for i in soup.find_all("a","_user-icon size-75 cover-texture ui-profile-popup"):
artists_id.append(i["data-user_id"])
return artists_id
pages=find_page()
for page in range(1,pages):
for i in find_artists_id(page):
print("正在下载画师id=%s"%i)
art_collect(i)
judge_user()
while True:
cmd=input("请选择模式\n(1:获取排行榜,2:tag搜索,3:个人收藏夹保存,4:画师图片提取,5:个人关注画师作品下载,6:退出)")
if cmd not in ["1","2","3","4","5","6"]:
print("错误输入!")
cmd=input("请重新输入")
if cmd=="1":
print("--------------------------------------------------进入排行榜获取模式--------------------------------------------------")
get_rank()
if cmd=="2":
print("--------------------------------------------------进入tag搜索模式--------------------------------------------------")
p_tag()
if cmd=="3":
print("--------------------------------------------------进入个人收藏夹保存模式--------------------------------------------------")
book_mark()
if cmd=="4":
print("--------------------------------------------------进入画师提取页面--------------------------------------------------")
art_id=input("请输入画师id信息:")
art_collect(art_id)
if cmd=="5":
print("--------------------------------------------------进入关注画师作品下载--------------------------------------------------")
find_artists()
if cmd=="6":
print("--------------------------------------------------正在退出,谢谢使用--------------------------------------------------")
break