天天看點

Python-網易雲熱榜音樂批量爬取 *我的郵箱是 [email protected] 大家有問題可以聯系我哦~*

Python-網易雲熱榜音樂批量爬取 *我的郵箱是 [email protected] 大家有問題可以聯系我哦~*

python

哈喽大家好!我是@xiaomeng @小孟

歡迎來到今天的小課堂 —— python-網易雲熱榜音樂批量爬取

Python-網易雲熱榜音樂批量爬取 *我的郵箱是 [email protected] 大家有問題可以聯系我哦~*

哈喽大家好!

 首先,我要先和大家講述一下這個爬取音樂的原理和方法操作:

   # 1.我們需要下載下傳第三方庫,(可以用cmd指令pip,或者直接在pycharm裡面安裝),代碼如下:

pip install requests
pip install lxml
           

    上面的指令放在cmd裡面直接運作好了!(我就不過多贅述了)

   ## 2.我們再用requests.get的句型,記得要加請求頭,不然可能會失敗。

import requests
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46'
}
res = requests.get(url,headers=headers)
print(res.text)
           

    ###3.擷取網頁源代碼

        這一步很簡單,直接print(res.text)就可以啦!

    ####4.構造xpath對象

data = etree.HTML(res.text)  # 先把網頁轉化成html
list = data.xpath('//a[contains(@href,"/song?")]')
           

    #####5.在源代碼裡面找到歌曲的id

for music in list:
    href = music.xpath('./@href')[0]
    base_url = 'http://music.163.com/song/media/outer/url?id='
    id = href.split("=")[1]
    
           

   關于 for……in: 的用法,可以檢視連結如下:

   https://m.py.cn/faq/python/11687.html

   ###### 6.拼接 網址+id

music_url = base_url+id
           

    #######7.下載下傳儲存

music_url = base_url+id
    music_mp3 = requests.get(music_url,headers=headers)
    with open('./music/'+name+'.mp3', 'wb') as f:
        f.write(music_mp3.content)
    print(name+'下載下傳成功')
           

這裡我找到了with open的用法,大家可以看一下,見下方連結:

https://blog.csdn.net/qq_34816007/article/details/105049033

完整代碼如下:

import requests
from lxml import etree  # 資料解析
import os
if os.path.exists('music') is False:
    os.mkdir('music')
url = 'https://music.163.com/discover/toplist?id=3778678'
# print(url)
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46'
}
res = requests.get(url,headers=headers)
print(res.text)
data = etree.HTML(res.text)
list = data.xpath('//a[contains(@href,"/song?")]')
for music in list:
    href = music.xpath('./@href')[0]
    base_url = 'http://music.163.com/song/media/outer/url?id='
    id = href.split("=")[1]
    # print(id)
    name = music.xpath('./text()')[0]
    music_url = base_url+id
    music_mp3 = requests.get(music_url,headers=headers)
    with open('./music/'+name+'.mp3', 'wb') as f:
        f.write(music_mp3.content)
    print(name+'下載下傳成功')
           

另外;我再多說幾句: 

Python-網易雲熱榜音樂批量爬取 *我的郵箱是 [email protected] 大家有問題可以聯系我哦~*

大家寫代碼的時要注意代碼規範,尤其python 縮進嚴格,新手很容易發生錯誤

今天的文章就到這裡啦!

下期再見!

(如果你喜歡這篇文章,可以在下方留言或者關注我哦,謝謝啦!)

Python-網易雲熱榜音樂批量爬取 *我的郵箱是 [email protected] 大家有問題可以聯系我哦~*

感謝大家的閱讀!

*我的郵箱是 [email protected] 大家有問題可以聯系我哦~*

@文章作者: 小孟

@文章(代碼)稽核: 小于派派