需求:
利用python爬取网站数据进行下载时,显示下载进度
# 设置下载进度条
tk.Label(window, text='下载进度:').place(x=40, y=80)
canvas = tk.Canvas(window, width=600, height=16, bg="white")
canvas.place(x=20, y=90)
# 下载按钮函数
def usr_download():
response = session.get(url_str, headers=headers2, cookies=cookies_xxx, verify=False, stream=True) # stream=True表示请求成功后并不会立即开始下载,而是在调用iter_content方法之后才会开始下载
chunk_size = 40960 # 设置每次下载的块大小
content_size = int(m4a.headers['content-length']) # 从返回的response的headers中获取文件大小
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
raise_data = 600 / (content_size/chunk_size) # 增量大小,600为进度条的长度
# 将下载的数据写入文件
with open(title + '.m4a', 'wb') a