pycharm爬取qq音乐排行榜代码
时间: 2024-12-19 07:26:17 浏览: 12
pycharm爬取51job网站
以下是使用Python和Pycharm爬取QQ音乐排行榜的代码:
```
import requests
import json
# 设置请求头
headers = {
'Referer': 'https://y.qq.com/n/yqq/toplist/4.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'
}
# 获取排行榜页面
response = requests.get('https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg?tpl=3&page=detail&date=2021_30&topid=4&type=top&song_begin=0&song_num=30&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0', headers=headers)
# 解析json数据
data = json.loads(response.text)
# 遍历歌曲列表
for music in data['songlist']:
# 获取歌曲mid
song_mid = music['data']['mid']
# 构造获取vkey的url
url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?callback=getplaysongvkey6354954596402418&g_tk=5381&jsonpCallback=getplaysongvkey6354954596402418&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%22583432000%22%2C%22songmid%22%3A%5B%22{}%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%220%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%7D'.format(song_mid)
# 发送获取vkey的请求
response = requests.get(url, headers=headers)
# 解析json数据
data = json.loads(response.text.strip('getplaysongvkey6354954596402418(').strip(')'))
# 获取vkey
vkey = data['req_0']['data']['midurlinfo'][0]['purl']
# 输出歌曲名和播放链接
print(music['data']['songname'], 'https://isure.stream.qqmusic.qq.com/{}?vkey={}&guid=538432000&uin=0&fromtag=66'.format(vkey, data['req_0']['data']['sip'][0]))
```
阅读全文