Everyone is responsible for supporting genuine
Project requirements
① Simulate the search and download functions in the QQ music web page:
- Enter the artist or song title to search out the related song list, and then optionally download.
- You can download songs locally. The naming method for downloading to the local is: song name + singer + id.
First, go to QQ Music to find the audio download interface:
Randomly search for a song:
Then open the developer tools of the Chrome browser and click to play the song:
Next, find the media type file. Obviously the music we want is the largest file. Click on it:
Open this URL:
So far, we have successfully found the audio download interface of QQ music.
Second, analyze the music download interface:
https://isure.stream.qqmusic.qq.com/C400003y3tQK2JZaau.m4a?
guid=4441071008&
vkey=6A1D99170713A9FFA58B61103D38450398E41C3ACDABF3F3E6FF170FB936B9239C1412F189015FE08484D930C1F11B39CD37EA7443CFC395&
uin=0&
fromtag=66
Let's analyze this song first songmid参数
:
We can see the song songmid参数
Shi 003y3tQK2JZaau
, is the URL of C400
the following sections.
songmid
From the naming point of view, it is the unique identifier of the song
The remaining parameters can be found on the audio file:
3. Next, analyze the search interface:
Open the search box, enter the song name, and intercept the search request:
Next, decode the URL:
Analyze the parameters in the URL to remove some unnecessary parameters:
https://c.y.qq.com/soso/fcgi-bin/client_search_cp?
ct=24&
qqmusic_ver=1298&
new_json=1&
remoteplace=txt.yqq.top&
searchid=35829660933924128&
t=0&
aggr=1&
cr=1&
catZhida=1&
lossless=0&
flag_qc=0&
p=1&
n=10&
w=你要相信这不是最后一天&
g_tk_new_20200303=1944343149&
g_tk=1944343149&
loginUin=0&
hostUin=0&
format=json&
inCharset=utf8&
outCharset=utf-8&
notice=0&
platform=yqq.json&
needNewCode=0
The keyword searched here is w参数
.
After removing unnecessary parameters, as follows:
https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w=
歌曲或歌手名称
Fourth, extract information through the search interface:
import requests
import json
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w=你要相信这不是最后一天'
response = requests.get(url, headers=headers)
js1 = json.loads(response.text.strip('callback()'))
js1 = js1['data']['song']['list']
print(js1)
Output:
In this way, we successfully extracted the song information.
Then build the list and save the relevant information:
medias = []
song_mid = []
src = []
song_names = []
singers = []
for rest in js1:
medias.append(rest['media_mid'])
song_mid.append(rest['songmid'])
song_names.append(rest['songname'])
singers.append(rest['singer'][0]['name'])
But we just extracted it from here songmid参数
. If you want to download the song, you have to extract it vkey参数
.
5. Obtain vkey parameters through the download interface:
https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?&jsonpCallback=MusicJsonCallback&cid=205361747&songmid=
Through this interface, we can obtain vkey参数
:
for n in range(0, len(medias)):
url2 = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?&jsonpCallback=MusicJsonCallback&cid=205361747&songmid=' + song_mid[n] + '&filename=C400' + medias[n] + '.m4a&guid=4441071008'
rest2 = get_request(url2)
# print(rest2.text)
js2 = json.loads(rest2.text)
print(js2)
Six, download songs through the download interface:
for n in range(0, len(medias)):
try:
url2 = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?&jsonpCallback=MusicJsonCallback&cid=205361747&songmid=' + song_mid[n] + '&filename=C400' + medias[n] + '.m4a&guid=4441071008' # 重要参数提取接口
rest2 = self.get_request(url2)
js2 = json.loads(rest2.text)
vkey = js2['data']['items'][0]['vkey']
src.append('https://isure.stream.qqmusic.qq.com/C400' + medias[n] + '.m4a?vkey=' + vkey + '&guid=4441071008&uin=0&fromtag=66') # 下载接口
except:
print("检测到异常,请重新下载")
break
print("*"*30)
for m in range(0, len(src)):
print("%-4d" % (int(m+1)) + '《' + song_names[m] + '》' + ' - ' + singers[m])
song_index = int(input("请选择序号:").strip())
if song_index < 0:
print("退出QQ音乐爬虫程序")
break
try:
song_data = self.get_request(src[song_index - 1])
data = song_data.content
self.download(data, song_names[song_index-1])
except Exception as er:
print("检测到异常,请重新下载 ", er)