GUI界面爬虫-壁纸批量下载

        什么?现在这年代还缺壁纸?壁纸批量下载工具适合你。。。。。

        彻底实现壁纸自由。。。。

from tkinter import *
from tkinter import ttk,filedialog,messagebox
import requests,re,time

class Downloadpucture(object):
    def __init__(self):
        #请求头
        self.headers={
        'Accept': 'text / html, application / xhtml + xml, application /'
        ' xml;q = 0.9, image / webp, image / apng, * / *;q = 0.8',
        'Accept - Encoding': 'gzip, deflate',
        'Accept - Language': 'zh - CN, zh;q = 0.9',
        'Cache - Control': 'max - age = 0',
        'Host': 'www.netbian.com',
        'Upgrade - Insecure - Requests': '1',
        'User - Agent': 'Mozilla / 5.0(WindowsNT10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / '
        '70.0.3538.25Safari / 537.36Core / 1.70.3870.400QQBrowser / 10.8.4405.400'
        }

        self.list=["rili","dongman","fengjing","meinv","youxi","yingshi","dongtai","weimei","sheji","keai","qiche","huahui",
              "dongwu","jieri","renwu","meishi","shuiguo","jianzhu","tiyu","junshi","feizhuliu","qita","wangzherongyao","huyan","lol"]


    # 这个函数是为了下面被调用时不传参使用的
    def choosevarise(self):
        global Break
        Break = "None"
        while True:
            try:
                if Folderpath is None:
                    pass
                else:
                    text.insert(END, "开始下载中。。。。。。。。。")
                    self.judge()
                    break
            except:
                UI().alert()

    def judge(self):
        global Url
        number=UI().num()
        kd = self.list[number]
        for i in range(1, self.picturepages(kd, number) + 1):
            if 0 <= number < 22:
                Url = "http://www.netbian.com/%s/index_%d.htm" % (kd, i)
                if i == 1:
                    Url = "http://www.netbian.com/%s/" % kd
                else:
                    pass
            elif 22 <= number <= 24:
                Url = "http://www.netbian.com/s/%s/index_%d.htm" % (kd, i)
                if i == 1:
                    Url = "http://www.netbian.com/s/%s/" % kd
                else:
                    pass
            try:
                if Break == "stop":
                    break
            except:
                pass
            self.picturenum()

    #获取图片;类型下所有图片的二级链接
    def indexdata(self):
        rep = requests.get(url=Url, headers=self.headers)
        return rep.text



    #正则提取出二级链接下响应页面的三级地址
    def picturenum(self):
        zz=r'href="/desk/.*?.htm"'
        data = re.findall(zz,self.indexdata())
        global URl
        for i in data:
            try:
                URl = 'http://www.netbian.com%s' % i[6:-1]
                self.picturenum2(URl)
                if Break == "stop":
                    break
                else:
                    pass
            except:
                pass

    #获取真实图片地址
    def picturenum2(self,url):
        page = requests.get(url, headers=self.headers)
        zz = re.findall(r'src=.*?alt',page.text)
        self.download(zz[0][5:-5])


    #获取图片所有页数,找到该图片类型下所有的页数
    def picturepages(self, kd,number):
        if 0<=number<22:
            req = requests.get(url="http://www.netbian.com/%s/" % kd, headers=self.headers).text
            gz = r'.htm">.*?</a><a href="/%s/index_2.htm'% kd
            NUM=re.findall(gz,req)[0].split(">")[-2]
            PAGE=re.match(r'\d{0,4}',NUM).group()
            return int(PAGE)
        else:
            req = requests.get(url="http://www.netbian.com/s/%s/" % kd, headers=self.headers).text
            gz = r'.htm">.*?</a><a href="/s/%s/index_2.htm'% kd
            NUM=re.findall(gz,req)[0].split(">")[-2]
            PAGE=re.match(r'\d{0,4}',NUM).group()
            return int(PAGE)




    # 下载图片
    def download(self,URL):
        D = requests.get(URL, stream=True)
        path = UI().returnargs() + "/" + URL[-10:-4] + ".jpg"
        with open(path, "wb") as f:
            f.write(D.content)
            text.insert(END,path + "下载成功!" + "\n")
            text.update() #为了保证信息实时输出,必须加上刷新,不然文本输出框会等所有信息加载完成后统一输出
            time.sleep(1)

   # 停止下载
    def BREAK(self):
        global Break
        Break = "stop"

    # 校验输入和选择是否有误
    def inputjudge(self):
        pass


class UI(object):
    def __init__(self):
        self.LIST=["0.日历","1.动漫","2.风景","3.美女","4.游戏","5.影视","6.动态","7.唯美","8.设计","9.可爱","10.汽车","11.花卉","12.动物",
              "13.节日","14.人物","15.美食","16.水果","17.建筑","18.体育","19.军事","20.非主流","21.其他","22.王者荣耀","23.护眼","24.LOL"]
    #主函数
    def main(self):
            global root
            root=Tk()
            self.args()
            self.lable()
            self.choose()
            self.output()
            self.button()
            root.mainloop()


    # 窗口参数
    def args(self):
        root.title("music download")
        root.geometry("1200x700")


    #按钮控制
    def button(self):
        button1=Button(root,text="开始",font=1,bg="yellow",comman=Downloadpucture().choosevarise)  #这是函数没有传递参数时的写法,
        # 需要传递参数时需要在函数前面添加lambda函数(参考下面这个函数)
        #button1=Button(root,text="开始下载",bg="yellow",comman=lambda:Downloadpucture().judge(number))
        button2=Button(root,text="选择保存路径",bg="yellow",comman=self.path) #在调用函数时注意函数后不能加括号
        button3=Button(root,text="暂停下载",font=1,bg="yellow",comman=Downloadpucture().BREAK)
        button1.place(x="430",y="580",height="80",width="150")
        button2.place(x="250",y="50",height="30",width="80")
        button3.place(x="620",y="580",height="80",width="150")

    #窗口输出
    def output(self):
        global text,text1
        text1=Text(root,font=1,fg="red")
        text1.place(height="30",width="210",x="30",y="50")
        text=Text(root,font=1,relief=FLAT,fg="blue")
        text.place(x="400",y="50",height="500",width="750")

    #文本标签
    def lable(self):
        lb=Label(root,text="请选择保存路径:",fg="red",bd=2)
        lb1=Label(root,text="请选择图片类型:",fg="red",bd=2)
        lb.place(height="30",width="120",x="30",y="20")
        lb1.place(height="30",width="120",x="30",y="120")



    #唤起系统文件路径选择
    def path(self):
            global Folderpath
            #root.withdraw()  # 隐藏实例,窗口会被隐藏
            Folderpath = filedialog.askdirectory()  # 获得选择好的文件夹,选择文件时把askdiretory()改为askfilename()
            text1.delete(0.0,END) # 注意,这儿清空文本框中的起始元素为浮点数,0.0表示为第一行的第0个元素
            text1.insert(END,Folderpath)
            STR="图片已保存在:%s" % Folderpath
            text.insert(END,STR + "\n")


    #选择图片类型
    def choose(self):
        global comvalue
        comvalue = StringVar()
        comboxlist = ttk.Combobox(root,textvariable=comvalue,font=1,state='readonly')
        comboxlist["values"] = self.LIST
        comboxlist.current(1)
        comboxlist.bind("<<ComboboxSelected>>",self.num)
        comboxlist.place(height="30", width="210", x="30", y="150")

    # 绑定的函数必定要上传参数,所以带了*args参数,为了实时上传
    def num(*args):
        text.insert(END,"你已选择图片类型:" + comvalue.get() + "\n")
        number=int(re.match(r'\d{0,4}',comvalue.get()).group())
        return number

    # 返回路径参数
    def returnargs(self):
        PATH=Folderpath
        return PATH


    def alert(self):
        messagebox.showinfo("提示","请选择保存路径!!!")
        self.path()


if __name__ == "__main__":
    UI().main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二九筒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值