python爬取音乐的源码
时间: 2024-12-08 15:21:53 浏览: 17
根据提供的引用内容,可以看出这篇文章主要介绍了使用Python爬取音乐软件API的方法,并提供了一个歌手信息爬虫的源码。以下是该爬虫的源码:
引用:
```
import requests
import json
def get_singer_info(singer_id):
url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg'
headers = {
'referer': 'https://y.qq.com/n/yqq/singer/{}.html'.format(singer_id),
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
params = {
'singerid': singer_id,
'order': 'listen',
'begin': 0,
'num': 30,
'songstatus': 1
}
response = requests.get(url, headers=headers, params=params)
json_data = response.text
data = json.loads(json_data)
song_list = data['data']['list']
for song in song_list:
print('歌曲名:', song['musicData']['songname'])
print('所属专辑:', song['musicData']['albumname'])
print('播放时长:', song['musicData']['interval'], '秒')
print('播放链接:', 'https://y.qq.com/n/yqq/song/{}.html'.format(song['musicData']['songmid']))
print('\n')
if __name__ == '__main__':
singer_id = '0025NhlN2yWrP4'
get_singer_info(singer_id)
```
该源码使用requests库向QQ音乐API发送请求,获取指定歌手的歌曲信息,并将结果打印输出。其中,singer_id为歌手的ID,可以在QQ音乐网站上找到。
阅读全文