1. 程式人生 > >【爬蟲入門】下載網易雲歌單中的歌曲到本地

【爬蟲入門】下載網易雲歌單中的歌曲到本地

from tkinter import *
import requests
from bs4 import BeautifulSoup
from urllib.request import urlretrieve


def download():
    url = entry.get()
    new_url = url.replace('/#', '')

    header = {
        'Host': 'music.163.com',
        'Referer': 'https://music.163.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
    }

    res = requests.get(new_url, headers=header).text

    r = BeautifulSoup(res, "html.parser")
    music_dict = {}
    result = r.find('ul', {'class', 'f-hide'}).find_all('a')
    for music in result:
        print(music)
        music_id = music.get('href').strip('/song?id=')
        music_name = music.text
        music_dict[music_id] = music_name
    for song_id in music_dict:
        song_url = "http://music.163.com/song/media/outer/url?id=%s" % song_id
        path = "E:\\英語\%s.mp3" % music_dict[song_id]

        # 新增資料
        text.insert(END, "正在下載:%s" % music_dict[song_id])
        text.see(END)
        text.update()
        try:
            urlretrieve(song_url, path)
        except Exception as e:
            pass


root = Tk()
root.title("網易雲音樂下載器")
root.geometry("550x400+550+230")

label = Label(root, text="歌單URL", font=('宋體', 15))
label.grid()

entry = Entry(root, font=('微軟雅黑', 20))
entry.grid(row=0, column=1)

text = Listbox(root, font=("微軟雅黑", 15), width=45, height=10)
text.grid(row=1, columnspan=2)

button = Button(root, text="開始下載", font=("微軟雅黑", 15), command=download)
button.grid(row=2, column=0, sticky=W)

button1 = Button(root, text="退出", font=("微軟雅黑", 15), command=root.quit)
button1.grid(row=2, column=1, sticky=E)

mainloop()