好的,以下是一个简单的
Python爬虫爬取QQ音乐歌曲的代码:
```
python
im
port requests
im
port json
def get_song_list(keyword, page):
获取歌曲列表
:param keyword:
搜索关键词
:param page: 页码
:return: 歌曲列表
url =
'https://c.y.qq.com/soso/fcgi-bin/client_search_cp
'
'referer
':
'https://y.qq.com/portal/search.html
',
'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,
'needNewCode
':
'0
'
song_list = []
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
def get_song_url(song_id):
获取歌曲播放链接
:param song_id: 歌曲ID
:return: 歌曲播放链接
url =
'https://u.y.qq.com/cgi-bin/musicu.fcg
'
'referer
':
'https://y.qq.com/
',
'-
':
'getplaysongvkey
' + str(int(round(time.time() * 1000))),
'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}}
'
if data[
'code
'] == 0:
return data[
'req_0
'][
'data
'][
'midurlinfo
'][0][
'purl
']
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)
本文地址:http://lianchengexpo.xrbh.cn/quote/6297.html
迅博思语资讯 http://lianchengexpo.xrbh.cn/ , 查看更多