python爬虫qq音乐歌曲
时间: 2024-01-13 22:03:57 浏览: 143
以下是使用Python爬取QQ音乐歌曲的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 定义爬取函数
def crawl_qq_music(artist, page):
url = f'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=70717595233868220&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p={page}&n=20&w={artist}&g_tk_new_20200303=5381&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'
# 发送请求
response = requests.get(url)
data = response.json()
# 解析数据
song_list = data['data']['song']['list']
for song in song_list:
song_name = song['name']
album_name = song['album']['name']
play_link = f"https://y.qq.com/n/yqq/song/{song['mid']}.html"
print(f"歌曲名:{song_name}")
print(f"专辑名:{album_name}")
print(f"播放链接:{play_link}")
print()
# 调用爬取函数
crawl_qq_music('周杰伦', 1)
```
这段代码使用了requests库发送HTTP请求,使用BeautifulSoup库解析HTML页面,从QQ音乐的接口获取指定歌手的歌曲信息,并打印出歌曲名、专辑名和播放链接。
阅读全文