爬取qq音乐排行榜代码
时间: 2023-10-05 14:08:07 浏览: 325
以下是爬取 QQ 音乐排行榜的 Python 代码,使用了 requests 和 BeautifulSoup 库。
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头
headers = {
'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'}
# 发送请求,并获取响应
url = 'https://y.qq.com/n/yqq/toplist/4.html'
response = requests.get(url, headers=headers)
html = response.text
# 解析 HTML
soup = BeautifulSoup(html, 'html.parser')
song_list = soup.select('.songlist__songname_txt')
# 输出歌曲名
for song in song_list:
print(song.text)
```
该代码将输出 QQ 音乐巅峰榜的前十名歌曲名。你可以根据自己的需求修改代码,例如获取更多的歌曲名或其他排行榜的歌曲。
相关问题
pycharm爬取qq音乐排行榜代码
以下是使用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]))
```
Python爬取QQ音乐排行榜的代码
好的,这个问题可以回答。以下是Python爬取QQ音乐排行榜的代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求,获取HTML页面内容
url = "https://y.qq.com/n/yqq/toplist/4.html"
response = requests.get(url)
html = response.text
# 用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, "html.parser")
song_list = soup.find_all("li", class_="js_song")
# 遍历歌曲信息,提取歌曲名字和演唱者
for song in song_list:
song_name = song.find("div", class_="songname").text.strip()
singer = song.find("div", class_="singer").text.strip()
print(song_name, "-", singer)
```
希望能帮到你!
阅读全文