鱼C论坛

 找回密码
 立即注册
查看: 2024|回复: 12

[作品展示] Twilight6爬妹子作品,加了个图形界面(18+)

[复制链接]
发表于 2020-6-15 15:50:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 java2python 于 2020-6-15 17:45 编辑

灌水回帖太多,找不到原帖,也不知道他的标题是18+,还是15+,选择顶级(18+)。。。
界面如下(操作:双击页码,右边listbox表示该页的作品,双击作品,右边listbox表示有多少图片,点击开始就下载选中的图片):
crawler.jpg

代码(请不要做产品测试,明明这里没选,去点击开始,出错了。。。):
  1. import requests
  2. import time
  3. import re
  4. import os
  5. import tkinter as tk
  6. from tkinter import *
  7. import threading


  8. class App:
  9.     base_url = 'https://www.mzitu.com/'
  10.     headers = {
  11.         'Referer': 'https://www.mzitu.com/',
  12.         'Sec-Fetch-Dest': 'image',
  13.         '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'
  14.     }
  15.     list_meizi_url = None
  16.     photo_sum = 0
  17.     sel_meizi_no = 0
  18.     os_path = os.getcwd()
  19.     #process_msg = None
  20.    
  21.     def __init__(self, top):
  22.         #top = Tk()
  23.         top.title('Heavy task')
  24.         top.geometry('1030x800')
  25.         top.resizable(False, False)
  26.         top.config(bg='#535353')

  27.         #
  28.         sb = Scrollbar(top)  # 设置滚动条组件
  29.         sb.grid(row=0, column=3, sticky=N + S)
  30.         page_list = Listbox(top,  # 设置IP列表
  31.                           yscrollcommand=sb.set,
  32.                           height=30, bg='black', fg='white',
  33.                           highlightcolor='black', font=('微软雅黑')
  34.                           )
  35.         page_list.grid(row=0, column=0, columnspan=2)
  36.         page_list.bind("<Double-Button-1>", self.sel_page)       # #左键双击某条元素,执行方法
  37.         sb.config(command=page_list.yview)  # 设置鼠标滚轮

  38.         meizi_list = Listbox(top,  # 设置IP列表
  39.                           #yscrollcommand=sb3.set,
  40.                           height=25, bg='black', fg='white',
  41.                           highlightcolor='black', font=('微软雅黑')
  42.                           )
  43.         meizi_list.place(x=200, y=0, width=500)
  44.         meizi_list.bind("<Double-Button-1>", self.sel_meizi)       # #左键双击某条元素,执行方法

  45.         photo_list = Listbox(top,  # 设置IP列表
  46.                           #yscrollcommand=sb3.set,
  47.                           height=25, bg='black', fg='white',
  48.                           highlightcolor='black', font=('微软雅黑')
  49.                           )
  50.         photo_list.place(x=700, y=0, width=200)
  51.         photo_list.bind("<Double-Button-1>", self.sel_photo)       # #左键双击某条元素,执行方法

  52.         text_process = Text(top,width = 120,height = 18,bg='black', fg='white')
  53.         text_process.place(x=200,y=555)
  54.         # 按钮
  55.         button_start = Button(top, text='开始', fg='#F5F5F5', bg='#7A7A7A', command=self.thread_process_show_start, height=1, width=15, relief=GROOVE, bd=2, activebackground='#F5F5F5', activeforeground='#535353')
  56.         button_start.place(x=900,y=100)
  57.         #App.thread_process_show_start()
  58.         
  59.         self.page_list = page_list
  60.         self.meizi_list = meizi_list
  61.         self.photo_list = photo_list
  62.         self.text_process = text_process

  63.     def heavy(self):
  64.         global app
  65.         app.text_process.delete("1.1" ,'end')
  66.         try:
  67.             if os.getcwd() != App.os_path:
  68.                 os.chdir(os.pardir)
  69.             os.mkdir(f'{App.list_meizi_url[App.sel_meizi_no][0]}')
  70.         except OSError:
  71.             pass
  72.         os.chdir(f'{App.list_meizi_url[App.sel_meizi_no][0]}')
  73.         save_original(App.list_meizi_url[App.sel_meizi_no][1],self.photo_list.curselection()[0])
  74.     def thread_process_show_start(self):
  75.         th = threading.Thread(target=self.heavy)
  76.         th.setDaemon(True)
  77.         th.start()
  78.         
  79.     def sel_page(self,no):
  80.         access_page(App.headers,None,self.page_list.curselection()[0]+1)
  81.         
  82.     def sel_meizi(self,no):
  83.         access_meizi(App.list_meizi_url[self.meizi_list.curselection()[0]][1],None)
  84.         
  85.     def sel_photo(self,no):
  86.         pass

  87.     @staticmethod
  88.     def print_msg(msg,end=None):
  89.         if end == None:
  90.             print(msg)
  91.             #App.process_msg = msg
  92.             app.text_process.insert('end',msg+'\n')
  93.         else:
  94.             print(msg,end='')



  95. # 获取所有IP
  96. def __ip(url, headers):
  97.     response = requests.get(url, headers=headers)
  98.     html = response.text
  99.     ip_list = re.findall(r'<td>(\d+\.\d+\.\d+\.\d+)</td>', html)
  100.     ip_port = re.findall(r'<td>(\d+)</td>', html)
  101.     _https = re.findall(r'<td class="country">(.+)</td>\s*<td>(\w+)</td>', html)
  102.     _ip = []
  103.     for i in range(len(ip_list)):
  104.         if _https[i][0] == '高匿' and _https[i][1] == 'HTTPS':
  105.             _ip.append(ip_list[i] + ':' + ip_port[i])
  106.     return _ip


  107. # 获取可用IP
  108. def _https():
  109.     ip_list = __ip('https://www.xicidaili.com/nn/', headers=headers)
  110.     count = 0
  111.     for i in ip_list:
  112.         count += 1
  113.         try:
  114.             proxies = {
  115.                 'https': f'http://{i}'
  116.             }
  117.             url = 'https://httpbin.org/get'
  118.             requests.get(url, headers=headers, proxies=proxies)
  119.         except:
  120.             App.print_msg(f'| --- 获取第{count}个IP({proxies["https"][7:]})失效...... \n| --- 继续尝试获取有效IP......')
  121.             continue
  122.         yield proxies


  123. # 访问首页
  124. def access(url, headers, ip=None):
  125.     global app
  126.     response = requests.get(url, headers=headers, proxies=ip)
  127.     html = response.text
  128.     page = re.findall(r'href=\'.+/page/(.+)/\'', html)[-1]
  129.     for p in range(1,int(page)+1):
  130.         app.page_list.insert(END, p)
  131.    
  132. def access_page(headers, ip,ask):
  133.     global app
  134.     url = f'https://www.mzitu.com/page/{ask}/'
  135.     response = requests.get(url, headers=headers, proxies=ip)
  136.     html = response.text
  137.     group = list(photo_url(html))
  138.     page = re.findall(r'aria-current=.+>(\d+)', html)[0]
  139.     app.meizi_list.delete(0, END)
  140.     App.list_meizi_url = []
  141.     for i in range(len(group)):
  142.         #print('| {:-^6}> {:<} '.format(i, group[i][0]))
  143.         app.meizi_list.insert(END,group[i][0])
  144.         App.list_meizi_url.append([group[i][0],group[i][1]])

  145. # 爬取图片url及图片标题
  146. def photo_url(html):
  147.     photo_urls = re.findall(r'<li><a href="(.+)" .+<img', html)
  148.     titles = re.findall(r'data-original=\'.+ alt=\'(.+)\' w', html)
  149.     return zip(titles, photo_urls)


  150. # 获取原图片url,并保存
  151. def access_meizi(meizi_url,ip):
  152.     global app
  153.     response = requests.get(meizi_url, headers=App.headers, proxies=ip).text
  154.     page = max(list(map(int, re.findall(meizi_url + r'/(\d+)', response))))
  155.     App.sel_meizi_no = app.meizi_list.curselection()[0]
  156.     App.photo_sum = page
  157.     app.photo_list.delete(0,END)
  158.     for p in range(page//9 + 1):
  159.         app.photo_list.insert(END,str(p*9)+"-"+str(min(p*9+8,App.photo_sum)))

  160.    
  161. def save_original(photo_url,photo_no):
  162.     for i in range(photo_no*9+1, min(photo_no*9+9,App.photo_sum)+1):
  163.         url = photo_url + f'/{i}'
  164.         rep = requests.get(url, headers=App.headers, proxies=None).text
  165.         add_page_photo = int(re.findall(r'><span>(\d+)</span></a>', rep)[-1])
  166.         try:
  167.             photo_urls = re.findall(r'<img class=.+ src="(.+\.jpg)" alt=', rep)
  168.             photo_urls[0]
  169.         except IndexError:
  170.             App.print_msg('| ---此图片专辑消失了,跳过此专辑下载...')
  171.             break
  172.         save_photo(photo_urls[0], App.list_meizi_url[App.sel_meizi_no][0], App.headers, i, add_page_photo, None)


  173. # 保存图片,显示下载进度条
  174. def save_photo(url, title, headers, count, page, ip=None):
  175.     with open(title + f'{count}.jpg', 'wb') as f:
  176.         response = requests.get(url, headers=headers, stream=True, proxies=ip)
  177.         chunk_size = 50
  178.         size = 0
  179.         content_size = int(response.headers['content-length'])
  180.         _temp = page-count
  181.         if _temp == -1:
  182.             _temp = 0
  183.         App.print_msg('[文件大小]{:.2f}KB  正在下载第 {} 张  本专辑还剩 {} 张'.format(content_size / (chunk_size*2*10.24),count,_temp))
  184.         start = time.time()
  185.         for data in response.iter_content(chunk_size=chunk_size):
  186.             f.write(data)
  187.             size += len(data)
  188.             App.print_msg('\r' + '| --- 下载进度 %s%.2f%%' % ('>' * int(size * 50 / content_size), float(size / content_size * 100)),
  189.                   end='')
  190.         end = time.time()
  191.         App.print_msg('   下载完成!用时%.2f秒' % (end - start))

  192. # 创建一个toplevel的根窗口,并把他作为擦参数实例化APP对象
  193. top = tk.Tk()
  194. app = App(top)
  195. access(App.base_url, App.headers, None)
  196. # 开始主事件循环
  197. top.mainloop()
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
Twilight6 + 5 + 5 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-15 17:12:33 | 显示全部楼层
@Twilight6 你的吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-15 17:15:32 | 显示全部楼层
xiaosi4081 发表于 2020-6-15 17:12
@Twilight6 你的吗?

部分源码是我的......GUI 是他自己做的....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-15 17:17:58 | 显示全部楼层
使用他人代码,应添加版权信息。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-15 17:31:08 | 显示全部楼层
本帖最后由 java2python 于 2020-6-15 17:48 编辑
赚小钱 发表于 2020-6-15 17:17
使用他人代码,应添加版权信息。


嗯,就是三楼的
已经改了标题,不过论坛一栏里:好像还是没改过来。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-19 21:13:28 | 显示全部楼层
帮你顶顶~~~ 实际上标题 很重要,加上回复可见就更好了  如果没设置回复可见很多人都直接白嫖了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-19 21:21:51 | 显示全部楼层
Twilight6 发表于 2020-6-19 21:13
帮你顶顶~~~ 实际上标题 很重要,加上回复可见就更好了  如果没设置回复可见很多人都直接白嫖了

其实我觉得吧,这种爬虫,GUI意义不大……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-19 21:22:54 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-6-19 21:21
其实我觉得吧,这种爬虫,GUI意义不大……

  能做出来 都棒棒的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-6-19 21:28:49 | 显示全部楼层
Twilight6 发表于 2020-6-19 21:13
帮你顶顶~~~ 实际上标题 很重要,加上回复可见就更好了  如果没设置回复可见很多人都直接白嫖了

谢谢大佬评分,实际不是在那人的帖子里发牢骚,主要是今天积分在239停住了(大概5,6个水贴,积分还是停在那里,看看咋回事),然后找个地方回帖,当然是水贴最好,找到哪里,点进去不知道该说什么,就说了点安慰的话,仅此而已。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-19 21:31:27 | 显示全部楼层
永恒的蓝色梦想 发表于 2020-6-19 21:21
其实我觉得吧,这种爬虫,GUI意义不大……


嗯,爬虫主要是简明(功能还是要达到的),
一方面让别人能够一看就知道该怎么学写爬虫。
另一方面是方便别人改,最好略微改一下就能够适应别的网站

感觉上是这样,其实不懂爬虫
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-19 21:40:39 | 显示全部楼层
嗯,看到了,哎。。。不知道该说啥,到网站还是要守规矩。聪明是好的,不该被用来耍。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-19 21:48:18 | 显示全部楼层
java2python 发表于 2020-6-19 21:28
谢谢大佬评分,实际不是在那人的帖子里发牢骚,主要是今天积分在239停住了(大概5,6个水贴,积分还是停在 ...

只有前十帖加分。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-19 21:50:32 | 显示全部楼层

谢谢,知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 04:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表