爬取网易云歌单的代码
时间: 2024-05-02 14:12:59 浏览: 75
以下是爬取网易云歌单的 Python 代码:
```python
import requests
import json
# 设置请求头
headers = {
'Referer': 'https://music.163.com/',
'Host': 'music.163.com',
'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'
}
# 获取歌单信息
def get_playlist(playlist_id):
url = 'https://music.163.com/api/playlist/detail?id={}'.format(playlist_id)
response = requests.get(url, headers=headers)
data = json.loads(response.text)
return data
# 解析歌单信息
def parse_playlist(data):
playlist_name = data['result']['name']
track_list = []
for track in data['result']['tracks']:
track_name = track['name']
artist_name = track['artists'][0]['name']
album_name = track['album']['name']
track_list.append((track_name, artist_name, album_name))
return playlist_name, track_list
# 测试代码
if __name__ == '__main__':
playlist_id = '123456' # 歌单ID
data = get_playlist(playlist_id)
playlist_name, track_list = parse_playlist(data)
print('歌单名称:', playlist_name)
print('歌曲列表:', track_list)
```
阅读全文