python中tkinter模块制作表情包爬取工具遇到的问题
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
image = requests.get(url=image_url, headers=headers).content
try:
print('正在下载')
with open(r'%s\%s' % (self.local, image_name + '.' + image_url.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()
之前有大佬说的
‘tkinter每一帧都不能因为你的程序阻塞,阻塞了就无响应’
不太懂又没有可以讲解一下 爬取程序会卡顿导致界面阻塞?分个线程给单独爬取 加个sleep一秒吧 高端 我不会图形化界面 import threading
..中间略过...
#线程,防卡
def thread_it(func, *args):
'''将函数打包进线程'''
# 创建
t = threading.Thread(target=func, args=args)
# 守护 !!!
t.setDaemon(True)
# 启动
t.start()
# 阻塞--卡死界面!
# t.join()
..中间略过..
self.download=tk.Button(self.root,text='开始下载',command=lambda :thread_it(self.download) ).place(x=300,y=0)
这个试试看~~ 化界面有点不懂 毛阿毛 发表于 2020-4-29 11:26
import threading
..中间略过...
谢谢大佬了,我对这个模块不太熟悉还得慢慢学
页:
[1]