写一个python爬虫爬取QQ音乐歌曲的代码
时间: 2023-11-23 18:08:10 浏览: 113
爬虫实战:爬取CSDN2022.6.21首页HTML代码
好的,以下是一个简单的Python爬虫爬取QQ音乐歌曲的代码:
```python
import requests
import json
def get_song_list(keyword, page):
"""
获取歌曲列表
:param keyword: 搜索关键词
:param page: 页码
:return: 歌曲列表
"""
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
headers = {
'referer': 'https://y.qq.com/portal/search.html',
'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 = {
'ct': '24',
'qqmusic_ver': '1298',
'new_json': '1',
'remoteplace': 'sizer.yqq.song_next',
'searchid': '64405487069162918',
't': '0',
'aggr': '1',
'cr': '1',
'catZhida': '1',
'lossless': '0',
'flag_qc': '0',
'p': page,
'n': '20',
'w': keyword,
'g_tk': '5381',
'loginUin': '0',
'hostUin': '0',
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': '0',
'platform': 'yqq.json',
'needNewCode': '0'
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
song_list = []
data = json.loads(response.text)
for song in data['data']['song']['list']:
song_info = {
'song_name': song['name'],
'singer': song['singer'][0]['name'],
'album': song['album']['name'],
'interval': song['interval'],
'song_id': song['mid']
}
song_list.append(song_info)
return song_list
else:
return None
def get_song_url(song_id):
"""
获取歌曲播放链接
:param song_id: 歌曲ID
:return: 歌曲播放链接
"""
url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
headers = {
'referer': 'https://y.qq.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'
}
params = {
'-': 'getplaysongvkey' + str(int(round(time.time() * 1000))),
'g_tk': '5381',
'loginUin': '0',
'hostUin': '0',
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': '0',
'platform': 'yqq.json',
'needNewCode': '0',
'data': '{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"1535846080","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"1535846080","songmid":["' + song_id + '"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}'
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = json.loads(response.text)
if data['code'] == 0:
return data['req_0']['data']['midurlinfo'][0]['purl']
else:
return None
else:
return None
if __name__ == '__main__':
keyword = '周杰伦'
page = 1
song_list = get_song_list(keyword, page)
for song in song_list:
song_url = get_song_url(song['song_id'])
print(song['song_name'], song['singer'], song['album'], song_url)
```
通过以上代码,我们可以获取到指定关键词下的歌曲列表,并且获取每首歌曲的播放链接。
阅读全文