对之前的表情包进行升级简单的制作一个界面但是存在一个小问题程序运行时会未响应,但不会影响表情包的爬取
[Python] 纯文本查看 复制代码
import requests
import re
import random
from tkinter.filedialog import askdirectory
from tkinter import *
#选择保存位置
class download():
@staticmethod
def selectPath():
path_ = askdirectory()
path.set(path_)
local=path_
return local
@staticmethod
#下载
def loaddown():
txt=Text(root)
txt.grid(row = 2, column = 0)
trouble=0
local_=download().selectPath()
while (1):
n=random.randrange(2,2600)
url=(r'http://www.doutula.com/photo/list/?page=%d'%n)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
result=requests.get(url=url,headers=headers).text
pattern=re.compile('data-original="(.*?)"\salt="(.*?)"\sclass=',re.S)
image_urls=re.findall(pattern,result)
for image_url in image_urls:
image_name=image_url[1]
image=requests.get(url=image_url[0],headers=headers).content
try:
with open(r'%s\%s'%(local_,image_name+'.'+image_url[0].split('.')[-1]),'wb')as file:
file.write(image)
except:
print('下载错误')
trouble+=1
s=str('下载错误\n下载错误%d张'%(trouble))
txt.insert(END,s)
root.update()
#print('页码%d下载完成\n下载错误%d张'%(n,trouble))
#界面
root=Tk()
root.geometry('460x240')
root.title('表情包下载')
path = StringVar()
#Label(root,text = "目标路径:").grid(row = 0, column = 0)
#Entry(root, textvariable = path).grid(row = 0, column = 1)
#Button(root, text = "路径选择", command = download.selectPath).grid(row = 0, column = 2)
btn1 = Button(root, text='开始下载', command=download.loaddown).grid(row = 1, column = 0)
root.mainloop()
|