1. 程式人生 > 實用技巧 >Python爬取網易雲音樂歌手歌曲和歌單

Python爬取網易雲音樂歌手歌曲和歌單

僅供學習參考

Python爬取網易雲音樂網易雲音樂歌手歌曲和歌單,並下載到本地

很多人學習python,不知道從何學起。
很多人學習python,掌握了基本語法過後,不知道在哪裡尋找案例上手。
很多已經做案例的人,卻不知道如何去學習更加高深的知識。
那麼針對這三類人,我給大家提供一個好的學習平臺,免費領取視訊教程,電子書籍,以及課程的原始碼!
QQ群:101677771


①找到要下載歌手歌曲的連結,這裡用的是:
https://music.163.com/#/artist?id=10559
要提前建好儲存資料夾:path1 = "D:/360下載/網易雲音樂/1/"
然後更改你要儲存的目錄,目錄要先建立好資料夾,例如我的是儲存在D盤-360下載-網易雲音樂-1資料夾內,就可以完成下載。如果資料夾沒有提前建好,會報錯[Errno 2] No such file or directory。

②找到要下載歌單的連結,這裡用的是:
https://music.163.com/#/playlist?id=5175828159
要提前建好儲存資料夾:path2 = "D:/360下載/網易雲音樂/2/"
只能下載前面10首。
之後的歌曲資訊伺服器不給資料,無法拿到歌曲id。
我嘗試使用網易雲音樂PC端(可以載入歌單所有歌曲),用fiddler進行抓包,是POST請求,通過模擬請求,得到的response是亂碼,嘗試utf-8、gbk、gbk2312等解碼也是亂碼。應該客戶端拿到資料是加密的,我沒有找到其解密方式。只能使用模擬網頁請求拿取歌單前面10首歌曲。
若有好的想法,可以一起探討。
程式碼寫於:2020.8.23

③要下載熱歌榜所有歌曲,請檢視我前一個釋出內容

from urllib import request
from bs4 import BeautifulSoup
import re
import requests
import time


class Music(object):
    def __init__(self, baseurl, path):
        head = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
            }
        baseurl = baseurl.replace("#/", "")
        self.baseurl = baseurl
        self.headers = head
        self.path = path


    def main(self):
        html = self.askurl()
        bs4 = self.analysis(html)
        id = self.matching(bs4)
        self.save(id)


    def askurl(self):
        req = request.Request(url=self.baseurl, headers=self.headers)
        response = request.urlopen(req)
        html = response.read().decode("utf-8")
        return html


    def analysis(self, html):
        soup = BeautifulSoup(html, "html.parser")
        bs4 = soup.find_all("li")
        bs4 = str(bs4)
        return bs4


    def matching(self, bs4):
        rule = re.compile(r'href="/song\?id=(\d*?)"', re.S)
        id = re.findall(rule, bs4)
        return id


    def save(self, id):
        for i in id:
            url = "https://music.163.com/song?id=" + i
            req = request.Request(url=url, headers=self.headers)
            response = request.urlopen(req)
            html = response.read().decode("utf-8")
            soup = BeautifulSoup(html, "html.parser")
            bs4 = soup.find_all("title")
            bs4 = str(bs4)
            rule = re.compile(r'<title>(.*?) - (.*?) - 單曲 - 網易雲音樂</title>', re.S)
            name = re.findall(rule, bs4)
            name = name[0]
            singername = name[1].replace(r"/", "_")
            print("正在下載:" + name[0] + " - " + singername + "……")
            saveurl = "http://music.163.com/song/media/outer/url?id=" + i
            content = requests.get(url=saveurl, headers=self.headers).content
            with open(self.path + name[0] + " - " + singername + ".mp3", "wb") as f:
                f.write(content)
            print(name[0] + " - " + singername + "-----------下載完畢。")
            time.sleep(1)
        return


if __name__ == "__main__":
    artisturl = "https://music.163.com/#/artist?id=10559"  # 下載歌手歌曲的url
    path1 = "D:/360下載/網易雲音樂/1/"  # 儲存路徑1
    artist_demo = Music(artisturl, path1)
    artist_demo.main()

    playlisturl = "https://music.163.com/#/playlist?id=5175828159"  # 下載歌單的url
    path2 = "D:/360下載/網易雲音樂/2/"  # 儲存路徑2
    playlist_demo = Music(playlisturl, path2)
    playlist_demo.main()
    print("\n全部歌曲下載完畢")