python爬取网易云歌曲

 

 只需要吧这个id替换掉代码中的id就可以了

# http://music.163.com/song/media/outer/url?id=为网易云的下载连接更换id即可
from lxml import etree
import requests
import json
from concurrent.futures import ThreadPoolExecutor

pool = ThreadPoolExecutor(max_workers=10)

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400"
}
def download(id,name):
    url = f'http://music.163.com/song/media/outer/url?id={id}'
    response = requests.get(url=url,headers = headers).content
    with open(name+'.mp3','wb') as f:
        f.write(response)
    print(name,'下载完成')
def get_id(url):

    response = requests.get(url=url,headers=headers).text
    page_html = etree.HTML(response)
    id_list = page_html.xpath('//textarea[@id="song-list-pre-data"]/text()')[0]
    for i in json.loads(id_list):
        name = i['name']
        id = i['id']
        author = i['artists'][0]['name']
        pool.submit(download,id,name+'-'+author)
    pool.shutdown()

if __name__ == '__main__':
    url = 'https://music.163.com/discover/toplist?id=3778678'#id可以自行更改
    get_id(url)

 

posted @ 2021-03-10 14:06  时光哥哥  阅读(282)  评论(0编辑  收藏  举报