帮我增加进度条import io import re import tkinter import requests import threading from pydub import AudioSegment root = tkinter.Tk() root.title('在线视频解析') root.geometry('500x590+550+350') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0'} ac = tkinter.Listbox(root, width=50, height=20, font=('黑体', 12)) ac.grid(row=2, columnspan=10, sticky="n" + "s" + "w" + "e") def sousuo(): i = b1.get() ac.delete(0, 'end') def extract_music_info(content): p = '<em>|</em>' content = re.sub(p, '', content, flags=re.S) pattern = re.compile('subject.*?href="(.*?)">(.*?)</a>', flags=re.S) return pattern.findall(content) def search_music(): url = 'https://www.hifini.com/search-' + i + '-1.htm' response = requests.get(url=url, headers=headers) return response.text def update_listbox(music_list): for music in music_list: pppp = music[1] + ":" + music[0] ac.insert('end', pppp) content = search_music() music_list = extract_music_info(content) update_listbox(music_list) def xiazzi(): def download_music(): ppp = ac.get(ac.curselection()) pp = re.search('thread.*?htm', ppp) v = pp.group() url1 = 'https://www.hifini.com/' + v response = requests.get(url=url1, headers=headers) ppp = response.text l2 = re.search('<script>.*?title:..(.*?).,.*?author:.(.*?).,.*?url:..(.*?).,', ppp, flags=re.S) p = 'https://www.hifini.com/' + l2.group(3) response = requests.get(url=p, headers=headers) l3 = response.content music_name = '{}-{}.mp3'.format(l2.group(2), l2.group(1)) if l3.startswith(b'\x00\x00\x00\x20\x66\x74\x79\x70'): audio = AudioSegment.from_file(io.BytesIO(l3), format='m4a') audio.export(music_name, format='mp3') else: with open(music_name, 'wb') as f: f.write(l3) print(music_name) threading.Thread(target=download_music).start() a1 = tkinter.Label(root, text='音乐下载器', anchor="center", font=('黑体', 24)) a1.grid(row=0, columnspan=10, sticky="n" + "s" + "w" + "e") b1 = tkinter.Entry(root, width=35, font=('黑体', 16), ) b1.grid(row=1, column=3, padx=15) search_button = tkinter.Button(root, text='搜索', command=sousuo) search_button.grid(row=1, column=4) download_button = tkinter.Button(root, text='下载', command=xiazzi) download_button.grid(row=3, column=4) root.mainloop()
时间: 2024-04-17 14:28:34 浏览: 169
在你提供的代码中,你希望在下载音乐的过程中增加一个进度条来显示下载进度。为了实现这个目标,我们可以使用 `tqdm` 库来创建一个进度条。
首先,确保你已经安装了 `tqdm` 库。可以使用以下命令来安装:
```shell
pip install tqdm
```
接下来,我们需要对 `download_music` 函数进行修改,以添加进度条。请将以下代码替换到 `download_music` 函数中:
```python
def download_music():
ppp = ac.get(ac.curselection())
pp = re.search('thread.*?htm', ppp)
v = pp.group()
url1 = 'https://www.hifini.com/' + v
response = requests.get(url=url1, headers=headers)
ppp = response.text
l2 = re.search('<script>.*?title:..(.*?).,.*?author:.(.*?).,.*?url:..(.*?).,', ppp, flags=re.S)
p = 'https://www.hifini.com/' + l2.group(3)
response = requests.get(url=p, headers=headers, stream=True) # 设置 stream=True 以启用流式下载
total_size = int(response.headers.get('Content-Length'))
music_name = '{}-{}.mp3'.format(l2.group(2), l2.group(1))
progress_bar = tqdm(total=total_size, unit='B', unit_scale=True) # 创建进度条
with open(music_name, 'wb') as f:
for data in response.iter_content(chunk_size=1024):
progress_bar.update(len(data)) # 更新进度条
f.write(data)
progress_bar.close() # 关闭进度条
print(music_name)
```
在上面的代码中,我们首先通过 `response.headers.get('Content-Length')` 获取文件的总大小,然后使用 `tqdm(total=total_size, unit='B', unit_scale=True)` 创建一个进度条。在每次迭代下载数据时,我们使用 `progress_bar.update(len(data))` 来更新进度条,并将数据写入文件。
这样,当你点击下载按钮时,将会显示一个进度条来显示下载进度。记得先导入 `tqdm` 库和修改 `download_music` 函数,然后重新运行代码。希望能帮到你!
阅读全文