一、打开网页https://music.163.com/,进入榜单(热歌榜)
二、右键检查、刷新网页,选择元素(点击歌曲名)
三、相关代码
import requests
#正则表达式模块内置模块
import re
import os
filename = 'music\\'
if not os.path.exists(filename):
os.mkdir(filename)
#如果想要爬取其他榜单的内容,只需要更改请求URL中的ID
url = 'https://music.163.com/discover/toplist?id=3778678'
#请求头
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
response = requests.get(url=url,headers=headers)
# print(response.text)
html_data = re.findall('(.*?)',response.text)
for num_id,title in html_data:
music_url = f'http://music.163.com/song/media/outer/url?id={num_id}.mp3'
#对于音乐播放地址发送请求,获取二进制数据内容
music_content = requests.get(url=url,headers=headers).content
with open(filename + title +'.mp3',mode='wb') as f:
f.write(music_content)
print(num_id,title)
四、爬取结果