python下载qq音乐
时间: 2024-04-16 18:23:28 浏览: 95
要下载QQ音乐,你可以使用Python的第三方库来实现。其中比较常用的是`requests`和`beautifulsoup4`库。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 搜索歌曲
def search_song(keyword):
url = f"https://c.y.qq.com/soso/fcgi-bin/client_search_cp?p=1&n=5&w={keyword}"
headers = {
"Referer": "https://y.qq.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
response = requests.get(url, headers=headers)
data = response.json()
song_list = data["data"]["song"]["list"]
for song in song_list:
print(f"歌曲名:{song['songname']}")
print(f"歌手:{song['singer']['name']}")
print(f"专辑:{song['albumname']}")
print(f"歌曲链接:https://y.qq.com/n/yqq/song/{song['songmid']}.html\n")
# 下载歌曲
def download_song(songmid):
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%2251234567890%22%2C%22songmid%22%3A%5B%22{songmid}%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%220%22%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A0%2C%22format%22%3A%22json%2C+jsonp%22%2C%22ct%22%3A24%2C%22cv%22%3A0%7D%7D"
headers = {
"Referer": "https://y.qq.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
response = requests.get(url, headers=headers)
data = response.json()
vkey = data["req_0"]["data"]["midurlinfo"]["vkey"]
song_url = f"http://dl.stream.qqmusic.qq.com/C400{songmid}.m4a?vkey={vkey}&guid=5123456789&uin=0&fromtag=66"
response = requests.get(song_url)
with open(f"{songmid}.m4a", "wb") as f:
f.write(response.content)
print("下载完成!")
# 示例:搜索歌曲并下载
keyword = input("请输入要搜索的歌曲名:")
search_song(keyword)
songmid = input("请输入要下载的歌曲的songmid:")
download_song(songmid)
```
请注意,这只是一个简单的示例代码,具体的实现可能会因为网站的更新而变化。你可以根据自己的需求进行修改和扩展。
阅读全文