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