写一个爬取qq音乐的程序
时间: 2024-05-18 11:18:50 浏览: 14
python爬虫:爬取某音乐平台的音乐
好的,以下是一个简单的爬取qq音乐的程序,使用Python语言编写:
```
import requests
from bs4 import BeautifulSoup
# 要爬取歌曲的名称
song_name = "周杰伦-稻香"
# 获取搜索结果页面
search_url = f"https://c.y.qq.com/soso/fcgi-bin/client_search_cp?p=1&n=20&w={song_name}"
search_response = requests.get(search_url)
# 提取搜索结果中的歌曲ID
search_soup = BeautifulSoup(search_response.text, "html.parser")
song_list = search_soup.find_all("a", {"class": "js_song"})
if len(song_list) == 0:
print("没有找到对应的歌曲!")
exit()
song_id = song_list[0]["data-songid"]
# 使用歌曲ID获取歌曲下载链接
download_url = f"https://u.y.qq.com/cgi-bin/musicu.fcg?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%22346850320%22%2C%22songmid%22%3A%5B%22{song_id}%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"
download_response = requests.get(download_url)
download_json = download_response.json()
download_link = download_json["req_0"]["data"]["midurlinfo"][0]["purl"]
if download_link.startswith("//"):
download_link = "https:" + download_link
print(f"歌曲《{song_name}》的下载链接是:{download_link}")
```
说明:
1. 首先通过 qq音乐的搜索接口获取对应歌曲的搜索结果页面;
2. 从搜索结果页面中提取出第一首歌曲的ID;
3. 使用获取到的歌曲ID请求歌曲下载链接接口,获取下载链接;
4. 输出歌曲名称和下载链接。
希望我的代码能够帮到您!
阅读全文