溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python怎么爬取網(wǎng)易云音樂

發(fā)布時間:2021-11-25 09:23:09 來源:億速云 閱讀:252 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“Python怎么爬取網(wǎng)易云音樂”,在日常操作中,相信很多人在Python怎么爬取網(wǎng)易云音樂問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么爬取網(wǎng)易云音樂”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

本次目標(biāo)

爬取網(wǎng)易云音樂

https://music.163.com/

Python怎么爬取網(wǎng)易云音樂

環(huán)境

  • python 3.6

  • pycharm

爬蟲代碼

導(dǎo)入工具

import requests
import re

請求網(wǎng)站、解析網(wǎng)站數(shù)據(jù)

def get_music_url(music_id, music_title):
    url = 'https://api.zhuolin.wang/api.php'
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Cookie': 'UM_distinctid=175aca5b31d39e-06d658eceb014a-3962420d-1fa400-175aca5b31e92e',
        'Host': 'api.zhuolin.wang',
        'Pragma': 'no-cache',
        'Referer': 'https://music.zhuolin.wang/',
        'Sec-Fetch-Dest': 'script',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
    }
    params = {
        'callback': 'jQuery111305698848623906863_1604919341715',
        'types': 'url',
        'id': '{}'.format(music_id),
        'source': 'netease',
        '_': '1604919341751',
    }
    response = requests.get(url=url, params=params, headers=headers)
    html_data = response.text
    if music_url == '':
        print('無音頻下載鏈接')

def music_id():
    url = 'https://music.163.com/discover/toplist'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    lis = re.findall('<li><a href="(.*?)">(.*?)</a></li>', response.text, re.S)[0:100]
    for i in lis:
        music_id = i[0].split('id=')[-1]
        title = i[1]
        pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]")  # '/ \ : * ? ">

保存數(shù)據(jù)

    else:
        path = '保存地址\\' + music_title + '.mp3'
        response = requests.get(url=music_url)
        with open(path, mode='wb') as f:
            f.write(response.content)
            print(music_title, music_url)

運行代碼,結(jié)果如下圖

Python怎么爬取網(wǎng)易云音樂

到此,關(guān)于“Python怎么爬取網(wǎng)易云音樂”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI