Lthero 发表于 2021-3-25 20:46

【WallHaven】批量下载工具_含GUI_python实现

本帖最后由 Lthero 于 2021-3-25 21:31 编辑

免费壁纸网站:https://wallhaven.cc/代码使用了
tkinter库、requests包、threading(防止窗口卡死)、beautifulsoup

尝试使用代{过}{滤}理proxy,但我失败了,会用的同学可以交流下!!!

GUI用tkinter实现的,调整控件位置太不方便,可能是我不会用......

步骤:
1、输入wallhaven网址    不同分类下的网址 如hot部分的 https://wallhaven.cc/hot
2、输入要下载的页数
3、选择保存的文件夹后自动开始下载

PS:
1、下载速度取决于图片大小和网速
2、图片均为原图像素
3、cookie和user-agent可以替换
4、ip好像有下载限制,30张不成问题
5、应该没多少人看,就当平时练习了

成品
链接: https://pan.baidu.com/s/19ZQG3fFwlgg4O_f65siAEA 提取码: v23k


import requests
from bs4 import BeautifulSoup
from random import choice
from tkinter.filedialog import askdirectory
from tkinter import messagebox, ttk
from tkinter import *
import threading

def download(url, name, file_path):
    headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4389.90 Safari/531.36'
      ,
      'Cookie': '_pk_id.1.01b8=cce9862fdcfc7513.1611905452.; remember_web_59ba36add22b2f9401580f014c7f58ea4e30989d=eyJpdiI6InZJNDZBaW5hVEFyVlwvMEROQU5GcjFRPT0iLCJ2YWx1ZSI6ImJGYkFDa3lyMzBRa1V6cFdVa3oyYTg2RFwvdURrWXU2V1ZqVEM4azRRTVN0ZnBKaEpDdjBcL1pvampJSHNId290S3VQQW1PY0FsbU4xdjlkM3hhb1Y0WXJVNzJCWTZYYUxEREdrN1FCZDBmVkhmclZ4TkRoZHZaN1AzZWdsdXRsZFM0ZmpOT1wvM1wvSzNQYXUrOUpTQUVOdUpcL0ZncTYxbDRoT2EyRDNxdTVzQW53NlhiaUhqK24zTnE5cHVKQmNnWWR1IiwibWFjIjoiODE5NjdlMWMxOWE1ODVjYmQzYzQyMzUzZWU5MmMyMWNmNWUyNjU2ZWZlOTgzYzEzODA2MDQ0NTlmZmQ2ZTc3NCJ9; __cfduid=d80f535c03e3d1a3b6bb69705f60b839f1616502624; _pk_ses.1.01b8=1; XSRF-TOKEN=eyJpdiI6ImNXeUViZmlnUDdHNmM4XC9wdm1IUDdBPT0iLCJ2YWx1ZSI6IkFiSzJTd1l5Mm9Jdks0WHBmVmg0NDA3SkJ2emc4QWNsMExjbnZUdjZBSUxyMlJYWkN6T0ZpXC92SGpwem91MkE1IiwibWFjIjoiZWYyOWMyMzJjNzM1ZDdlZjQxN2Q5NmEwMzQ0ZmJmZWYzMzY0YTA3ODkxOThkZWE5NDlmOTVmZDY3NTgwYTA2YiJ9; wallhaven_session=eyJpdiI6InMzTlF1WThGWmpoZzduTEJMT2hyZHc9PSIsInZhbHVlIjoiRWpyZW9JWmw1YlZOYlZhXC9na0FPTW91Rk5NdG1ZT2tpaTlUdUpPODU4S25GTmJYN093d0Jlb0pRR3M0NnRmb1kiLCJtYWMiOiI3OGUzMmEyMjkzMzA1MmZiMWM4MmQ1NGU3MGVkZDY0M2FiOWVlMzBjYzJjNWNjNmY4MGI3YWNkZDk2N2VjYWUyIn0%3D'}
    with open('%s/%s.jpg' % (file_path, name), 'wb') as f:
      img = requests.get(url, headers=headers).content
      f.write(img)
      f.close()

def open_url(url):
    headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4389.90 Safari/531.36'
      ,
      'Cookie': '_pk_id.1.01b8=cce9862fdcfc7513.1611905452.; remember_web_59ba36add22b2f9401580f014c7f58ea4e30989d=eyJpdiI6InZJNDZBaW5hVEFyVlwvMEROQU5GcjFRPT0iLCJ2YWx1ZSI6ImJGYkFDa3lyMzBRa1V6cFdVa3oyYTg2RFwvdURrWXU2V1ZqVEM4azRRTVN0ZnBKaEpDdjBcL1pvampJSHNId290S3VQQW1PY0FsbU4xdjlkM3hhb1Y0WXJVNzJCWTZYYUxEREdrN1FCZDBmVkhmclZ4TkRoZHZaN1AzZWdsdXRsZFM0ZmpOT1wvM1wvSzNQYXUrOUpTQUVOdUpcL0ZncTYxbDRoT2EyRDNxdTVzQW53NlhiaUhqK24zTnE5cHVKQmNnWWR1IiwibWFjIjoiODE5NjdlMWMxOWE1ODVjYmQzYzQyMzUzZWU5MmMyMWNmNWUyNjU2ZWZlOTgzYzEzODA2MDQ0NTlmZmQ2ZTc3NCJ9; __cfduid=d80f535c03e3d1a3b6bb69705f60b839f1616502624; _pk_ses.1.01b8=1; XSRF-TOKEN=eyJpdiI6ImNXeUViZmlnUDdHNmM4XC9wdm1IUDdBPT0iLCJ2YWx1ZSI6IkFiSzJTd1l5Mm9Jdks0WHBmVmg0NDA3SkJ2emc4QWNsMExjbnZUdjZBSUxyMlJYWkN6T0ZpXC92SGpwem91MkE1IiwibWFjIjoiZWYyOWMyMzJjNzM1ZDdlZjQxN2Q5NmEwMzQ0ZmJmZWYzMzY0YTA3ODkxOThkZWE5NDlmOTVmZDY3NTgwYTA2YiJ9; wallhaven_session=eyJpdiI6InMzTlF1WThGWmpoZzduTEJMT2hyZHc9PSIsInZhbHVlIjoiRWpyZW9JWmw1YlZOYlZhXC9na0FPTW91Rk5NdG1ZT2tpaTlUdUpPODU4S25GTmJYN093d0Jlb0pRR3M0NnRmb1kiLCJtYWMiOiI3OGUzMmEyMjkzMzA1MmZiMWM4MmQ1NGU3MGVkZDY0M2FiOWVlMzBjYzJjNWNjNmY4MGI3YWNkZDk2N2VjYWUyIn0%3D'}
    list_ip = ['61.145.212.31', '59.124.224.180', '117.26.41.218', '183.6.183.35', '117.65.47.142']
    proxy = choice(list_ip)
    proxies = {
      "http": "http://{}".format(proxy), "https": "https://{}".format(proxy)
    }
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    html = response.text
    return html


if __name__ == '__main__':
    root = Tk()
    root.title('wallhaven壁纸简单下载器')
    file = ['']
    Width = 350
    Height = 200
    down_numbers=0
    canvas = Canvas(root, width=Width, height=Height, highlightthickness=0, borderwidth=0)
    canvas.create_rectangle(0, 0, 550, 300, outline='yellow', fill="pink")
    canvas.place(x=0, y=0)
    root.resizable(height=False, width=False)
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (Width, Height, (screenwidth - Width) / 2, (screenheight - Height) / 2)
    root.geometry(alignstr)
    root.focus_set()
    canvas.create_text(64,48,text='网址')
    canvas.create_text(64, 88, text='要下载几页')
    e1 = Entry(root)
    e1.place(x=125, y=36)
    e2 = Entry(root)
    e2.place(x=125, y=76)
    def helpme():
      # 说明
      win2=Toplevel(root)
      win2.title('说明文档')
      width = 550
      height = 200
      screenwidth = root.winfo_screenwidth()
      screenheight = root.winfo_screenheight()
      alignstr2 = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
      win2.geometry(alignstr2)
      win2.resizable(width=False,height=False)
      win2.focus_set()
      Help=Label(win2,text='''步骤:
1、输入wallhaven网址不同分类下的网址 如hot部分的 https://wallhaven.cc/hot
2、输入要下载的页数
3、选择保存的文件夹后自动开始下载''',font=('微软雅黑',10))
      Help.place(x=40,y=40)

    def about():
      #关于
      win2 = Toplevel(root)
      win2.title('关于')
      width = 200
      height = 200
      screenwidth = root.winfo_screenwidth()
      screenheight = root.winfo_screenheight()
      alignstr2 = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
      win2.geometry(alignstr2)
      win2.resizable(width=False, height=False)
      win2.focus_set()
      Help = Label(win2, text='lthero制作\n\n喜欢可收藏', font=('微软雅黑',10))
      Help.place(x=70, y=60)


    # 头部菜单
    menu = Menu(root, tearoff=False)
    Cmenu = Menu(menu, tearoff=False)
    Cmenu.add_command(label='说明', command=helpme)
    Cmenu.add_separator()
    Cmenu.add_command(label='关于',command=about)
    menu.add_cascade(label='菜单', menu=Cmenu)
    root.config(menu=menu)

    def thread_it(func, *args):
      t = threading.Thread(target=func, args=args)
      t.setDaemon(True)
      t.start()

    def get_start(temp_url, number, file_path):
      mod = '?page='
      url =
      if int(number) >= 2:
            url.append(temp_url)
            for i in range(2, int(number) + 1):
                url.append(temp_url + mod + str(i))
      collection = []

      for x in url:
            content = open_url(x)
            soup = BeautifulSoup(content, 'lxml')
            images = soup.find('section', class_="thumb-listing-page")
            for li in images.find_all('li'):
                string = str(li.a['href'])
                collection.append(string)
      count = 0
      #print(file_path)
      for i in collection:
            canvas.create_text(55, 133, text='已经下载张数 ')
            canvas.create_text(99, 133, text=count)
            name = i.split('/')[-1]
            each_url = 'https://w.wallhaven.cc/full/%s/wallhaven-%s.jpg' % (name, name)
            #print(each_url)
            download(each_url, name, file_path)
            count += 1
            canvas.create_rectangle(5, 100, 140, 240, fill="pink",outline='pink')
            #if count % 20 == 0:
                #print('下载 ', count, ' 张了')


    def choice_file():
      file = askdirectory()
      messagebox.showinfo('选取成功', file)
      try:
            get_start(e1.get(),e2.get(),file)
            messagebox.showinfo('!!!!','下载完成!!!')
      except(requests.exceptions.ProxyError):
            messagebox.showinfo('到达今日下载上限')

    ttk.Button(root, text='选择文件夹', command=lambda: thread_it(choice_file)).place(x=150, y=120)
    root.mainloop()

Hangjau 发表于 2021-3-29 09:52

Hangjau 发表于 2021-3-25 21:16
楼主这怕是爬的NSFW

请改为NFSWnot work 模式 那个爬起来才有意思

PpaPingggg 发表于 2021-3-25 20:54

给力 谢谢楼主开源

Hangjau 发表于 2021-3-25 21:16

楼主这怕是爬的NSFW

Lthero 发表于 2021-3-25 21:22

Hangjau 发表于 2021-3-25 21:16
楼主这怕是爬的NSFW

我下载的hot页面{:1_899:}

zjytrhy 发表于 2021-3-25 21:48

下载试试hot页面:lol

额微粒波地 发表于 2021-3-25 23:52

这个壁纸网站这真的不错,老早就收藏了

第一品霄 发表于 2021-3-25 23:57

学习一下了

xztyx 发表于 2021-4-16 11:15

顶一个,,正好需要~!

Double611 发表于 2021-11-21 18:24

多谢楼主,赞一个
页: [1] 2
查看完整版本: 【WallHaven】批量下载工具_含GUI_python实现