Python爬虫系列16-采集网易音乐好听的百万歌曲下载

2022-08-23  本文已影响0人  Tony_Pm

序言

业精于勤而荒于嬉,行成于思而毁于随。
吃得苦中苦,方为人上人,受得累中累,方成富贵人。完全赞同

实战

image.png

第一步:请求网络链接先获取到网站返回数据


import requests
from lxml import etree

cookies = {
    '_ntes_nnid': 'aa83197e2faf6f06a6599baf24b3418f,1636546158090',
    'NMTID': '00O_4a1Ksfjt8lY1UEkriEl1Ik5yAEAAAF-i9Ao1Q',
    'WNMCID': 'uhexut.1643026186736.01.0',
    'WEVNSM': '1.0.0',
    'WM_TID': '8cq4tB6m0mBBFVUREVdvjxZEswHWwVXc',
    '_ntes_nuid': 'aa83197e2faf6f06a6599baf24b3418f',
    '_iuqxldmzr_': '32',
    'ntes_kaola_ad': '1',
    'JSESSIONID-WYYY': 'I2U9%5CTv%5Cog%5CXZ%5CgroWkmFHoowf8pMgXEqf13F3F5fEDAGWMlAMq50jqRADXvvCsdRshDN6T%5CaGVWEIMZXxbIMXiK5NwlW5E3dmWqujPacyXYUtbat1O9j8RV8vK%2F2QP93Av3YX3nenYXhcyu8DG8BgZapJRG5nj5o2qQDZw%5CVnVWJak4%3A1658580273713',
    'WM_NI': 'mZ3qnftT0NW1rAaRMxmMbpsOdDTMMvyvUX87SciskowqIITK2VyGzPos0kF0EEHeHhcqn4xED0Rteh6xE%2Fk%2Bn3xVdxBWmU4e3K22W1ZrHmuKXdeasKUzmRwfSC5JpMbNSjY%3D',
    'WM_NIKE': '9ca17ae2e6ffcda170e2e6ee99c87ea2bea7b2c96b838e8bb6d85f829b9e83c54987b796d0b7799888e188cc2af0fea7c3b92aaf86fd92ed6eb88c00afd63bb29fafb3d074af93ae91f245a89c9caed07ebbaa9bb9bb3d91bda794ea65b5af87aad741f28ab7a8f95cadad008cc53c879b8886cb64a1afa2d5fb4f88f09e95e864f6a88bd0ea21aeedb8b7e7748289a3b6b76ff195fed3d63b9aae888ef8419cbcab82f24197a6a3b3f66e919dfbabf347a2929d8bdc37e2a3',
}

headers = {
    'authority': 'music.163.com',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-language': 'zh-CN,zh;q=0.9','referer': 'https://music.163.com/',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'iframe',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'same-origin',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}

params = {
    'id': '4980157066',
}

response = requests.get('https://music.163.com/playlist', params=params, cookies=cookies, headers=headers)
print(response.text)

image.png

第二步:通过xpath进行数据解析

加载的数据 = etree.HTML(response.text)

提取的数据 = 加载的数据.xpath('//ul[@class="f-hide"]/li/a/@href')

print(提取的数据)

image.png

第三步:持久化 保存数据

替换 = tony.replace('/song?id=','')
   
网址MP3 = f'https://link.hhtjim.com/163/{替换}.mp3'
print(网址MP3)
mp3 = requests.get(网址MP3, params=params, cookies=cookies, headers=headers)
# 打开某个文件
file = open('Tony老师的音乐.mp3','wb')
file.write(mp3.content)

image.png

结果展示

image.png

在这个浮躁的时代;竟然还有人能坚持篇篇原创;

如果本文对你学习有所帮助-可以点赞👍+ 关注!将持续更新更多新的文章。

支持原创。感谢!

上一篇下一篇

猜你喜欢

热点阅读