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
|
''' 加入了顶部颜色的更改,实现了排它思想 下载按钮在下载中的按钮颜色更改 加上了 一个顶部的转台信息栏 加入了多线程功能,解决了在下载过程中图形界面卡顿问题 TODO 显示预览图 '''
from tkinter import * from tkinter import ttk from tkinter.filedialog import askdirectory from requests import get from re import findall from re import search from re import sub from os import listdir from os import mkdir from os import path from urllib.request import Request from urllib.request import urlopen from time import sleep from threading import Thread
class Tk_gui():
def __init__(self): ''' 初始化魔术方法 用于设置界面的初始状态 ''' self.root = Tk() self.root.title('一起来看美女') self.root.minsize(780, 370) self.root.maxsize(780, 570)
self.curr_page = 1 self.page_max = self.get_page_max() self.curr_url = 'https://www.mzitu.com/' self.tit_list = s.page_one(self.curr_url)
self.main_logic()
def thread_it(self, func, *args): ''' 将函数打包进线程 :param func: :param args: :return: ''' t = Thread(target=func, args=args) t.setDaemon(True) t.start()
def get_save_path(self):
path_ = askdirectory() Tk_gui.path.set(path_)
def get_page_max(self): return 50
def main_logic(self): ''' 主业务逻辑 :return: ''' topp = Frame() topp.grid(row=0, column=0) self.cont = Frame() self.cont.grid(row=1, column=0) self.indo = Frame() self.indo.grid(row=0, column=1, rowspan=2)
self.stat = Frame() self.stat.grid(row=2, column=0)
self.top_menu(topp) self.stat_info() self.main_content(self.get_num_list())
self.get_title_list('https://www.mzitu.com')
self.root.mainloop()
def download(self): ''' 正式下载图片 :return: ''' self.btn5['background'] = 'pink' para = self.search() s.ori_video(para[0], para[1], para[2])
def search(self): ''' 搜索预加载页面中的图片个数 :return: ''' index = int(self.number_chosen.get()) - 1 url = tit_list[0][index] para = s.pre_download(url) self.detail_info["text"] = "本页有" + str(para[0]) + "张图" return para
def get_title_list(self, url): ''' 获取标题列表内容 :param url: :return: ''' global tit_list tit_list = s.page_one(url) self.info["text"] = "\n".join(tit_list[1])
def get_num_list(self): ''' 获取数值列表 :return: ''' num = [] for i in range(1, 25): num.append(str(i)) num_str = "\n".join(num) return num_str
def set_top_bg(self, id): '''
:param id: id 是按钮摆放的位置,int类型的 :return: ''' for i in self.button_list: i['background'] = 'skyblue'
self.button_list[id - 1]['background'] = 'pink' pass
def update_page_url(self, option, reg_url): if option: if option == 2: print('下一页') if self.curr_page < self.page_max: self.curr_page += 1 elif option == 1: print('上一页') if self.curr_page > 1: self.curr_page -= 1 elif option == 3: self.curr_page = int(self.page_chosen.get()) if reg_url: self.curr_page = 1 self.curr_url = reg_url if self.curr_page == 1: url = self.curr_url else: url = self.curr_url + 'page/' + str(self.curr_page) + '/' print(self.curr_page) self.page_info['text'] = '当前页数:' + str(self.curr_page) self.get_title_list(url)
def top_menu(self, topp): ''' 用于绘制顶部菜单 :param topp: :return: ''' self.button_list = [] url_list = { '推荐': 'https://www.mzitu.com/', '性感': 'https://www.mzitu.com/xinggan/', '日本': 'https://www.mzitu.com/japan/', '台湾': 'https://www.mzitu.com/taiwan/', '清纯': 'https://www.mzitu.com/mm/' } menu = Button(topp, text='', font=("Arial", 12), width=1) menu.grid(row=0, column=0)
def put_button(key, col): ''' 用于画出按钮组件 :param contain:按钮显示的文本内容 :param key: 命令执行的函数选择的键 :param col: 按钮放置的列数 :return: ''' self.menu = Button(topp, text=key, font=("Arial", 12), width=7, \ command=lambda: self.update_page_url(option=0, reg_url=url_list[key]) is \ self.set_top_bg(col))
self.menu.grid(row=0, column=col) self.button_list.append(self.menu) for i in self.button_list: i['background'] = 'skyblue' self.button_list[0]['background'] = 'pink'
i = 1 for contain in url_list.keys(): put_button(contain, i) i += 1
self.page_info = Label(topp, text='', justify="left", \ font=("Arial", 12), width=10) self.page_info.grid(row=0, column=6)
self.page_info["text"] = '当前页数:1'
def main_content(self, num_str): ''' 主要内容界面,包括数字列表的显示,标题列表的显示以及右边输入框和下载按钮部分 :param num_str:数字列表显示内容 :return: '''
one = Label(self.cont, text=num_str, font=("Arial", 12), relief='ridge') one.grid(row=1, column=0) ''' relief(可选值为:flat(默认),sunken,raised,groove,ridge),borderwidth(边框的宽度,单位是像素,默认根据系统而定,一般是1或2像素) ''' self.info = Label(self.cont, text='info', justify="left", \ font=("Arial", 12), width=49, relief='ridge') self.info.grid(row=1, column=1, columnspan=6) ttk.Label(self.indo, text="\n\n\n选择一个", font=("Arial", 12)).grid(column=0, row=4, columnspan=2) number = StringVar() self.number_chosen = ttk.Combobox(self.indo, width=12, textvariable=number) self.number_chosen['values'] = tuple(range(1, 25)) self.number_chosen.grid(column=0, row=5, columnspan=2) self.number_chosen.current(0)
btn4 = Button(self.indo, text="查询图片个数", font=("Arial", 12), background='gray', command=lambda: self.thread_it(self.search)) btn4.grid(column=0, row=6, columnspan=2)
self.detail_info = Label(self.indo, text='', justify="left", \ font=("Arial", 12), width=10) self.detail_info.grid(row=7, column=0, columnspan=2) self.btn5 = Button(self.indo, text="下载此页图片", font=("Arial", 12), background='gray', command=lambda: self.thread_it(self.download)) self.btn5.grid(column=0, row=12, columnspan=2)
Tk_gui.terminal_info = Label(self.stat, text='', justify="left", \ font=("Arial", 12)) Tk_gui.terminal_info.grid(row=0, column=0)
Tk_gui.terminal_info["text"] = ''
menu = Button(self.indo, text=" 上一页", font=("Arial", 12), background='gray', command=lambda: self.thread_it(lambda: self.update_page_url(option=1, reg_url=''))) menu.grid(row=0, column=0)
menu = Button(self.indo, text="下一页 ", font=("Arial", 12), background='gray', command=lambda: self.thread_it(lambda: self.update_page_url(option=2, reg_url=''))) menu.grid(row=0, column=1)
page = StringVar() self.page_chosen = ttk.Combobox(self.indo, width=12, textvariable=page) self.page_chosen['values'] = tuple(range(1, self.page_max + 1)) self.page_chosen.grid(row=2, column=0, columnspan=2) self.page_chosen.current(0)
menu = Button(self.indo, text=" 跳转到此页 ", font=("Arial", 12), background='gray', command=lambda: self.update_page_url(option=3, reg_url='')) menu.grid(row=3, column=0, columnspan=2)
Tk_gui.path = StringVar() Label(self.indo, text="目标路径:", font=("Arial", 12)). \ grid(row=9, column=0, columnspan=2) Entry(self.indo, textvariable=self.path, width=15). \ grid(row=10, column=0, columnspan=2) Button(self.indo, text="更改下载路径", font=("Arial", 12), background='gray', command=self.get_save_path). \ grid(row=11, column=0, columnspan=2)
Tk_gui.path.set(path.dirname(__file__))
def stat_info(self): Tk_gui.stat_info = Label(self.stat, text='', justify="left", \ font=("Arial", 12)) Tk_gui.stat_info.grid(row=0, column=0)
Tk_gui.stat_info["text"] = ''
class Spider():
def pre_download(self, meizi_url): self.headers = { 'Referer': 'https://www.mzitu.com', 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36' } meizi_html = get(meizi_url, headers=self.headers).text end_url = findall("<span>(\d+?)</span></a><a href='https://www.mzitu.com/\d*?/2'><span>下一页»", meizi_html) name_id = search('<h2 class="main-title">(.*?)</h2>', meizi_html).group(1) name = sub('!', '', name_id) print("本页的总数:") end_url = int(end_url[0]) print(end_url) return end_url, name, meizi_url
def ori_video(self, end_url, name, meizi_url): choose_dir = Tk_gui.path.get()
curr_dir = choose_dir + '/' + name for x in range(1, end_url + 1): Tk_gui.terminal_info["text"] = \ '正在下载图片...\t' + '当前进度:' + str(x) + ' / ' + str(end_url)
if name not in listdir(choose_dir): mkdir(curr_dir) Tk_gui.terminal_info["text"] = '创建文件夹:' + curr_dir self.single(x, name, meizi_url, curr_dir) Tk_gui.terminal_info["text"] = '图片下载完成,成功保存到' + curr_dir[0:25] + '...'
def single(self, x, name, meizi_url, curr_dir): newurl = meizi_url + '/' + str(x)
html = get(newurl, headers=self.headers).text print(html) end_re = findall(r'<div class="main-image"><p><a href=".+?" ><img class="blur" src="(.*?)"', html) print("end_re>>>",end_re)
req = Request(end_re[0], headers=self.headers) response = urlopen(req) fname = end_re[0].split('/')[-1] print('正在下载: ', fname)
with open(curr_dir + '/' + fname, 'wb') as f: f.write(response.read()) ''' 反反爬 设置时间间隔 防止服务器封锁IP或禁止访问'''
def page_one(self, url): ''' # 下载一页的内容 :param url: :return: ''' headers = { 'Referer': 'https://www.mzitu.com', 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36' } html = get(url, headers=headers).text ret = findall('<li><a href="(.*?)" target="_blank"><img class=', html) tit = findall("<li><a.*?alt=\'(.*?)\'", html) return ret, tit
s = Spider() t = Tk_gui()
|