GUI Based on Tkinter

GUI is the visualization of software page
Let’s talk about label first

l = tk.Label(window, text='OMG! this is TK!', bg='green', font=('Arial', 12), width=15, height=2)
l.pack()

在这里插入图片描述
Label the window

Then let’s see how to input the information

# e = tk.Entry(window, show="*")
e = tk.Entry(window, show="1")
e.pack()

在这里插入图片描述
If it’s an *, it’s like a password. It’s invisible

And then take a look at listbox

lb = tk.Listbox(window, listvariable=var2)
list_items = [1,2,3,4]
for item in list_items:
    lb.insert('end', item)
lb.insert(1, 'first')
lb.insert(2, 'second')
lb.delete(2)
lb.pack()

在这里插入图片描述

And then take a look at RadioButton

r1 = tk.Radiobutton(window, text='Option A',
                    variable=var, value='A',
                    command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B',
                    variable=var, value='B',
                    command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C',
                    variable=var, value='C',
                    command=print_selection)
r3.pack()

在这里插入图片描述

And then take a look at scale

s = tk.Scale(window, label='try me', from_=5, to=11, orient=tk.HORIZONTAL,
             length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection)
s.pack()

在这里插入图片描述
Based on the above, we encapsulate a crawler of Baidu picture into a GUI
Prepare the crawler code first

def Imgurl(word,num):
    rep_list = []
    #模拟浏览器,需要用到浏览器的信息和目标url
    header = {
        'User-Agent': '',#在这里写自己浏览器的User-Agent
        "referer": "https://image.baidu.com"
    }
    #将中文关键字加密成浏览器能识别的乱码
    content= urllib.parse.quote(word,encoding='utf-8')
    #依据pn的规律从30到121循环4次,间隔为30
    for num in range(30,num+31,30):
        gsm = hex(num)[2:]         #将十进制数num转换成16进制数并取后两位
        url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord='+content+'&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&word='+content+'&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=&fr=&pn='+str(num)+'&rn=30&gsm='+ gsm +'&1521707235798='    #根据规律每次循环生成正确的请求地址
        req = urllib.request.Request(url=url,headers=header)   #获得请求对象
        page = urllib.request.urlopen(req).read()     #请求并读取返回信息
        try:              #如果返回信息遇到不在utf-8范围内的字符,跳过
            response = page.decode('utf-8')                       #解码返回的信息
            imgpattern = re.compile(r'"thumbURL":"(.*?)\.jpg')    #编写正则
            rsp_data = re.findall(imgpattern, response)           #通过正则匹配
            rep_list += rsp_data
        except UnicodeDecodeError:
            pass
    return rep_list

Request web page, get URL

Download pictures

def download_img(word,num):
    # 下载图片
    x = 1  # 计数
    img_urllist = Imgurl(word,num)
    img_path = Imgpath(word)
    for url in img_urllist[:num]:                  #循环提取Imgurl列表中的前100个字符串
        pngurl = url.replace(r'"thumbURL":"', " ")        #获得字符串里面的url
        path = img_path + '\\' + word + str(x) + '.png'   #下载图片的路径
        pngdata = urllib.request.urlopen(pngurl).read()   #下载图片数据
        f = open(path, 'wb')                              #必须用二进制写入
        f.write(pngdata)                                  #下载图片
        f.close()
        x += 1

Then write GUI.

win = tkinter.Tk()
win.title("百度图片爬虫")
win.geometry("400x200+400+200")
entry1 = tkinter.Entry(win,width=28) #e就代表输入框这个对象
entry2= tkinter.Entry(win,width=28)
entry1.insert(10,"请输入关键词")
entry2.insert(10,"请输入关键词")

#bind   给控件绑定事件
entry1.bind("<Button-1>",func1)
entry2.bind("<Button-1>",func1_)
#按钮BUTTON
#command 关联函数   注意 = 后加函数名,但是并不用加()
button2 = tkinter.Button(win,text="确定",command = func2,
                   width=6,height=1)
#显示出来
entry1.place(x=100,y=50)
entry2.place(x=100,y=80)
button2.place(x=170,y=120)
win.mainloop()

The effect is as follows
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值