tkinter与爬虫的结合-抓取网站图片程序解析

最近用空闲时间随手写了个抓取网站图片的图形化界面程序,已经开源于GitHub,地址如下:https://github.com/code-nick-python/scrapy-images,今天给大家解析一下这个程序:

#引入爬虫库
from bs4 import BeautifulSoup
import requests

#引入其他库
import time
import os
import validators

#引入tkinter系列库
import tkinter
import tkinter.messagebox
from tkinter.filedialog import askdirectory

#选择文件夹的函数
def selectPath():
    path_ = askdirectory()
    path.set(path_)

#使用第三方库判断是否为url
def is_url(url):
    if validators.url(url)==True:
        return True
    else:
        return False

#爬虫主程序
def scrapy():

    #从输入框中获取网址和存储地址
    url=link_entry.get()
    path=save_entry.get()

    #尝试请求
    try:
        response = requests.get(url, timeout=20)
        
    #请求失败弹出错误框
    except:
        tkinter.messagebox.showerror('wrong!','can not get the url\n请求网址失败')
        return
        
    #解析网页找到图片
    soup = BeautifulSoup(response.text, 'lxml')
    img_list=soup.find_all('img')
    
    #没有图片则弹出信息框
    if img_list==[]:
        tkinter.messagebox.showinfo('wrong!','no images on the website\n在网站上未发现图片')
        return

    #获取src图片链接属性
    for img in img_list:
        img_url=img['src']

        #尝试获取图片并写入路径
        try:
            img_get=requests.get(img_url,timeout=20)
            name=int(time.time())
            f = open(path+'\\'+str(name)+choice.get(),'wb')
            f.write(img_get.content)
            f.close()
            
        #无法获取时加上https:再次尝试
        except:
            try:
                img_url_retry='https:'+img_url
                img_get = requests.get(img_url_retry, timeout=20)
                name = int(time.time())
                f = open(path + '\\' + str(name) + choice.get(), 'wb')
                f.write(img_get.content)
                f.close()

            #无法获取则跳过
            except:
                continue

    #弹出消息框获取成功
    tkinter.messagebox.showinfo('success!','successfully download images!')
        
#判断是否是网址和存在路径
def is_entry_right():
    if is_url(link_entry.get()) & os.path.exists(save_entry.get()):
        scrapy()
    else:
        tkinter.messagebox.showwarning('wrong!','wrong url or path\n错误的地址或路径')

#创建窗口
top=tkinter.Tk()

#存储地址选择
path = tkinter.StringVar()

#设置窗口标题和大小
top.title('scrapy img')
top.geometry('300x210+25+25')

#版本说明
version=tkinter.Label(top,text='version:1.0')
version.pack()

#默认存储格式设置
choice=tkinter.StringVar()
choice.set('.png')

#开始抓取按钮
button=tkinter.Button(top,text="start scrapy images",command=is_entry_right)
button.pack()

#网站地址
link_add=tkinter.Label(top,text='scrapy url:')
link_add.pack()

#网站地址输入
link_entry=tkinter.Entry(top,width=50)
link_entry.pack()

#保存地址
save_add=tkinter.Label(top,text='save path:')
save_add.pack()

#输入地址
save_entry=tkinter.Entry(top,width=50,textvariable=path)
save_entry.place(x=0,y=120)

#选择地址按钮
path_choose=tkinter.Button(top,text='choose path',command=selectPath)
path_choose.place(x=110,y=145)

#图片存储格式选择
choose_jpg=tkinter.Radiobutton(top,text='.jpg',variable=choice,value='.jpg')
choose_jpg.place(x=25,y=180)
choose_png=tkinter.Radiobutton(top,text='.png',variable=choice,value='.png')
choose_png.place(x=125,y=180)
choose_png=tkinter.Radiobutton(top,text='.gif',variable=choice,value='.gif')
choose_png.place(x=225,y=180)

#开始主循环
top.mainloop()

这里就做一个简单的解析,其实并不难(当然功能也比较简陋,不过还能用用)

喜欢的可以点个关注哦!

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值