下载情况
[Python] 纯文本查看 复制代码 import tkinter as tk
from tkinter.filedialog import askdirectory
import requests
import random
import re
class picture():
def __init__(self):
self.root = tk.Tk()
self.root.title('表情包下载器')
self.root.geometry('460x230')
#self.local = ''
self.txt=tk.Text(self.root).place(x=0,y=50)
self.seachlocal=tk.Button(self.root,text='选择位置',command=self.selectPath).place(x=0,y=0)
self.download=tk.Button(self.root,text='开始下载',command=self.download).place(x=300,y=0)
self.root.mainloop()
def selectPath(self):
path_ = askdirectory()
self.local=path_
def download(self):
trouble = 0
while (1):
trouble = 0
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="(.*?)".*?alt="(.*?)"\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:
print('正在下载')
with open(r'%s\%s' % (self.local, image_name + '.' + image_url[0].split('.')[-1]), 'wb')as file:
file.write(image)
print('下载成功')
except:
print('下载错误')
trouble += 1
print('页码%d下载完成\n下载错误%d张' % (n, trouble))
#s = str('下载错误\n下载错误%d张' % (trouble))
if __name__ == '__main__':
picture()
|