整体步骤分三步:
1.设计tkinter页面布局
2.获取图片链接列表
3.执行下载操作
先放代码
import urllib
import re
import requests
try:
from tkinter import *
except ImportError: #Python 2.x
PythonVersion = 2
from Tkinter import *
from tkFont import Font
from ttk import *
from tkMessageBox import *
class GetbaiduPic:
def __init__(self):
self.root = Tk()
self.root.title('自动下载百度图片')
self.root.geometry('800x700')
#定义第一个输入框 输入关键字
self.var = StringVar()
self.flm = Frame(self.root).pack()
Label(self.flm,text='请输入关键字').pack()
self.e = Entry(self.flm,validate='key',textvariable=self.var).pack()
#定义第二个输入框 输入获取张数
self.var2 = StringVar()
self.flm2 = Frame(self.root).pack()
Label(self.flm2,text='请输入获取的张数,输入60的倍数').pack()
self.e2 = Entry(self.flm2,validate='key',textvariable=self.var2).pack()
#定义第三个输入框 输入保存路径
self.var3 = StringVar()
self.flm3 = Frame(self.root).pack()
Label(self.flm3,text='请输入要保存的路径,例如(D:\pic\),当前目录必须存在').pack()
self.e3 = Entry(self.flm3,validate='key',textvariable=self.var3).pack()
#设置button,回调下载函数
self.button = Button(self.root,text='开始',command=self.print_contone).pack()
#设置输入内容函数,返回内容
self.S = Scrollbar(self.root)
self.l = Text(self.flm2,width=100,height=20)
self.S.pack(side=RIGHT, fill=Y)
self.l.pack(side=RIGHT, fill=Y)
self.S.config(command=self.l.yview)
self.l.config(yscrollcommand=self.S.set)
#获取图片列表
def gethtml(self):
self.jpg_list = []
rn = '60'#获取张数 最多只能取60
pn = 0#开始取的数0为0-30张 120
word = self.var.get()
coneton = word.encode('utf-8') #转换成str类型
try:
for i in range(self.b):#获取输入的数字进行循环判断取多少条数据
url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result'+'&queryWord='+ coneton + '&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=' + coneton +'&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn='+ str(pn) +'&rn='+ rn +'&gsm=1e&1528178649573='
page = urllib.urlopen(url)
html = page.read()
pattin_pic = '"thumbURL":"(.*?)",'
pic_list = re.findall(pattin_pic,html,re.S)
for i in pic_list:
self.jpg_list.append(i)
pn += 60 #每次修改为60的倍数
return self.jpg_list
except:
print "没有获取到图片信息,请排查原因"
#执行下载操作
def down_pic(self):
a = self.var2.get()#在这一步获取输入的值
self.b = int(a)/60#判断b需要遍历几次,遍历值为60的倍数
path = self.var3.get()
newpath = path.encode('utf-8')#处理为UTF-8格式
self.print_content = []
self.gethtml()
for i,picurl in enumerate(self.jpg_list):#加下标循环 图片列表
try:
pic = requests.get(picurl, timeout=10)
string = newpath + str(i + 1) + '.jpg'#当前目录新建文件夹,并重新命名
with open(string,'wb') as f:#开始执行下载操作
f.write(pic.content)
# print '成功下载第%s张图片:%s' %(str(i + 1),str(picurl))
self.print_content.append('成功下载第%s张图片:%s' %(str(i + 1),str(picurl))+ '\n')
except Exception as e:
# print '下载第%s张图片失败:%s' %(str(i + 1),str(picurl))
self.print_content.append('下载第%s张图片失败:%s' %(str(i + 1),str(picurl))+ '\n')
print e
continue
def print_contone(self):
self.down_pic()
for itam in self.print_content:
self.l.insert(END,itam)
a = GetbaiduPic()
# a.down_pic()
mainloop()
查看百度图片页面,发现获取数据是请求的一个Url,每次下拉页面,变更的参数只有rn和pn
故推断出rn参数为每次获取的张数,最多支持获取60张,pn代表开始获取的张数,也可以理解为开始的索引(例如为pn=30,那就是取31张以后的图片)
下一步就是获取返回数据中的图片url,请求request URl,发现返回数据中非常有规律,都是在thumbURL里面,那就非常简单了,直接正则匹配,获取图片链接
获取到图片链接后,就开始执行 下载图片操作了, 直接使用requests.get 遍历下载图片,并保存到对应文件夹,至此,下载图片功能 大功告成啦~~
在下一步就是tk的布局了,这里也非常简单,就不多赘述了~~
最终效果图:
以上,如果有哪里表述的不清楚,可以直接留言,看到后会第一时间回复~