-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsankaku.py
339 lines (310 loc) · 13.4 KB
/
sankaku.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
import tkinter as tk
import tkinter.messagebox
import asyncio, aiohttp, aiofiles, os, threading
from time import sleep
from datetime import datetime
from lxml import etree
from tqdm import tqdm
from bs4 import BeautifulSoup
def rewrite(file, item):
'''从文件里移除文本'''
with open(file, "r+") as f:
data = f.read()
f.seek(0)
f.truncate()
f.write(data.replace(f'{item}\n', ''))
class first():
'''爬排行榜,获取id'''
def __init__(self):
b1['state'] = 'disabled'
self.ids = []
self.n = 0
self.start_date = e1.get()
self.end_date = e2.get()
self.url = f'https://idol.sankakucomplex.com/?tags=date%3A{self.start_date}..{self.end_date}%20order%3Aquality'
start = datetime.now()
try:
asyncio.run(self.main())
except (aiohttp.client_exceptions.ClientConnectionError, asyncio.exceptions.TimeoutError, RuntimeError):
tkinter.messagebox.showerror(title='错误', message='网络连接中断,请再次尝试第一步')
return
print(f'用时{datetime.now() - start}')
tkinter.messagebox.showinfo(title='Hi!', message='第一步已完成')
b1['state'] = 'normal'
async def main(self):
async with aiohttp.connector.TCPConnector(limit=300, force_close=True, enable_cleanup_closed=True, ssl=False) as tc:
async with aiohttp.ClientSession(connector=tc) as session:
self.create_txt()
await self.get_id(session, self.url)
self.save_id()
async def get_id(self, session, url):
'''爬取图片id'''
try:
response = await session.get(url, headers=header)
except:
print("连接失败,10秒后重新连接...")
await asyncio.sleep(10)
response = await session.get(url, headers=header)
if self.n == 0:
if response.status == 200:
print('HTTP:200 连接成功')
else:
print(f'HTTP:{response.status} 连接失败')
return
print(f'开始解析第{self.n + 1}页...')
html = await response.text()
bf = BeautifulSoup(html, 'lxml')
classes = bf.find_all('span', {'class':'thumb blacklisted'})
for i in classes:
self.ids.append(i['id'].split('p')[-1])
# 获取下一页的Query String Parameters
tree = etree.HTML(html)
try:
if self.n == 0:
next_page = tree.xpath('//*[@id="post-list"]/div[3]/div[1]/@next-page-url')[0]
else:
next_page = tree.xpath('/html/body/div/@next-page-url')[0]
except:
return
self.n += 1
next_url = 'https://idol.sankakucomplex.com/post/index.content' + next_page
await asyncio.sleep(3)
# n为下拉次数,既一个月份的页数
if self.n < 30:
await self.get_id(session, next_url)
def create_txt(self):
'''首次运行创建文本文件'''
if not os.path.exists('id.txt'):
f = open('id.txt', 'a')
f.close()
if not os.path.exists('all id.txt'):
f = open('all id.txt', 'a')
f.close()
if not os.path.exists('href.txt'):
f = open('href.txt', 'a')
f.close()
def save_id(self):
'''保存图片id并去重'''
n = 0
with open("all id.txt", "r") as d:
old_ids = d.read().splitlines()
print(f"已爬取{len(old_ids)}次")
with open("id.txt", "a") as e:
with open("all id.txt", "a") as f:
f.write(f'{self.start_date}..{self.end_date}:\n')
for i in self.ids:
if i not in old_ids:
n += 1
e.write(i + "\n")
f.write(i + "\n")
print(f"新增{n}个id")
class second():
'''通过id获取url'''
def __init__(self):
b2['state'] = 'disabled'
self.bad_ids = []
self.run_main()
b2['state'] = 'normal'
def run_main(self):
start = datetime.now()
try:
asyncio.run(self.main())
except (aiohttp.client_exceptions.ClientConnectionError, asyncio.exceptions.TimeoutError, RuntimeError):
sleep(10)
self.run_main()
print(f'用时{datetime.now() - start}')
tkinter.messagebox.showinfo(title='Hi!', message='第二步已完成')
async def main(self):
async with aiohttp.connector.TCPConnector(limit=300, force_close=True, enable_cleanup_closed=True, ssl=False) as tc:
async with aiohttp.ClientSession(connector=tc) as session:
with open('id.txt', 'r') as f:
self.ids = f.read().splitlines()
print(f'即将爬取{len(self.ids)}个url...')
sem = asyncio.Semaphore(4)
tasks = [self.get_href(session, sem, img_id) for img_id in self.ids if img_id]
await asyncio.gather(*tasks)
for _ in range(3):
if self.bad_ids:
print('开始重新获取...')
tasks = [self.get_href(session, sem, img_id, fail=True) for img_id in self.bad_ids]
await asyncio.gather(*tasks)
else:
break
async def get_href(self, session, sem, img_id, fail=False):
'''获取url'''
async with sem:
url = 'https://idol.sankakucomplex.com/post/show/' + img_id
try:
response = await session.get(url, headers=header)
except:
# 请求失败等待20秒再次请求
print('连接失败,20秒重新连接...')
await asyncio.sleep(20)
response = await session.get(url, headers=header)
if response.status != 200:
# 请求过于频繁,等待150秒
print("请求过于频繁,2分钟后重新开始")
if not fail:
self.bad_ids.append(img_id)
await asyncio.sleep(120)
return
tree = etree.HTML(await response.text())
try:
href = 'https:' + tree.xpath('//*[@id="image"]/@src')[0]
if fail:
self.bad_ids.remove(img_id)
except IndexError:
print("请求过于频繁,2分钟后重新开始")
if not fail:
self.bad_ids.append(img_id)
await asyncio.sleep(120)
return
print(href)
# 保存一份在href.txt
async with aiofiles.open('href.txt', 'a') as f:
await f.write(href + '\n')
await asyncio.sleep(0.1)
# 将已爬的img_id从id.txt中删除
rewrite("id.txt", img_id)
await asyncio.sleep(1)
class third():
'''通过url下载文件'''
def __init__(self):
b3['state'] = 'disabled'
self.n = 0
self.name = f'{e1.get()}..{e2.get()}'
self.download_header = {
'Sec-Fetch-Dest': 'document',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
self.run_main()
b3['state'] = 'normal'
def run_main(self):
start = datetime.now()
try:
asyncio.run(self.main())
except (aiohttp.client_exceptions.ClientConnectionError, asyncio.exceptions.TimeoutError, RuntimeError):
sleep(10)
self.run_main()
print(f'用时{datetime.now() - start}')
tkinter.messagebox.showinfo(title='Hi!', message='第三步已完成')
async def main(self):
async with aiohttp.connector.TCPConnector(limit=300, force_close=True, enable_cleanup_closed=True, ssl=False) as tc:
async with aiohttp.ClientSession(connector=tc) as session:
# 新建文件夹
self.new_dir()
# 读取href
with open('href.txt', 'r') as f:
hrefs = f.read().splitlines()
# 下载
sem = asyncio.Semaphore(10)
tasks = [self.download(session, sem, href) for href in hrefs if href]
await asyncio.gather(*tasks)
# 失败重新下载
with open("fail.txt", "r") as f:
fail_url_list = f.read().splitlines()
if fail_url_list:
print('开始重新下载...')
tasks = [self.download(session, sem, href, fail=True) for href in fail_url_list]
await asyncio.gather(*tasks)
def new_dir(self):
'''新建文件夹'''
self.b = os.path.abspath('.') + os.sep + self.name + os.sep
self.mp4 = os.path.abspath('.') + os.sep + self.name + os.sep + 'mp4' + os.sep
self.webm = os.path.abspath('.') + os.sep + self.name + os.sep + 'webm' + os.sep
self.gif = os.path.abspath('.') + os.sep + self.name + os.sep + 'gif' + os.sep
if not os.path.exists(self.b):
os.makedirs(self.b)
if not os.path.exists(self.mp4):
os.makedirs(self.mp4)
if not os.path.exists(self.gif):
os.makedirs(self.gif)
if not os.path.exists(self.webm):
os.makedirs(self.webm)
async def download(self,session, sem, href, fail=False):
'''下载'''
async with sem:
if '.jpg?' or '.png?' in href:
filename = self.b + href.split('=')[-1] + '.jpg'
if '.mp4?' in href:
filename = self.mp4 + href.split('=')[-1] + '.mp4'
#return
if '.gif?' in href:
filename = self.gif + href.split('=')[-1] + '.gif'
if '.webm?' in href:
filename = self.webm + href.split('=')[-1] + '.webm'
#return
response = await session.get(href, headers=self.download_header)
try:
file_size = int(response.headers['content-length'])
# 大于20M的文件不下载
if file_size > 20000000:
return
except:
rewrite("href.txt", href)
return
else:
if os.path.exists(filename):
# 读取文件大小
first_byte = os.path.getsize(filename)
if first_byte >= file_size:
print(f"已存在:{href}")
rewrite("href.txt", href)
return
# 从断点继续下载
download_header_ = {
'Sec-Fetch-Dest': 'document',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'Range': f'bytes={first_byte}-{file_size}'}
response = await session.get(href, headers=download_header_)
else:
first_byte = 0
try:
with open(filename, 'ab') as f:
f.write(await response.content.read())
self.n += 1
print(f"第{self.n}个文件下载完成")
if fail:
rewrite("fail.txt", href)
rewrite("href.txt", href)
except:
if not fail:
with open("fail.txt", "a") as f:
f.write(href + "\n")
header = {
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Host': 'idol.sankakucomplex.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
class Application(tk.Tk):
def __init__(self):
super().__init__()
global b1, b2, b3, e1, e2
self.title('Sankaku') # 给窗口的可视化起名字
self.geometry('400x200') # 设定窗口的大小(长 * 宽)
# 文字
l1 = tk.Label(self, text='开始日期:',font=('pingfang', 12))
l1.place(x=80,y=30,anchor='s')
l2 = tk.Label(self, text='结束日期:',font=('pingfang', 12))
l2.place(x=260,y=30,anchor='s')
# 输入框
e1 = tk.Entry(self, show=None)
e1.place(x=110,y=60,anchor='s')
e2 = tk.Entry(self, show=None)
e2.place(x=290,y=60,anchor='s')
# 按钮
b1 = tk.Button(self, text='第一步', font=('pingfang', 12), width=12, height=2, command=lambda :self.thread_it(first))
b1.place(x=80,y=180,anchor='s')
b2 = tk.Button(self, text='第二步', font=('pingfang', 12), width=12, height=2, command=lambda :self.thread_it(second))
b2.place(x=200,y=180,anchor='s')
b3 = tk.Button(self, text='第三步', font=('pingfang', 12), width=12, height=2, command=lambda :self.thread_it(third))
b3.place(x=320,y=180,anchor='s')
@staticmethod
def thread_it(func):
'''打包进线程'''
t = threading.Thread(target=func)
t.setDaemon(True)
t.start()
if __name__ == "__main__":
Application().mainloop()