1. 程式人生 > 其它 >Python爬蟲QQ音樂資料採取,公開資料獲取案例之一

Python爬蟲QQ音樂資料採取,公開資料獲取案例之一

工具準備

資料來源:QQ音樂
開發環境:win10、python3.7
開發工具:pycharm、Chrome

效果展示

專案思路解析

搜尋你需要的歌名或者歌曲
抓取對應的資料包

提取json資料裡的歌曲名字,歌曲的mid,歌手名字

for i in range(1, 10):
    url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
    params = {
        "ct": " 24",
        "qqmusic_ver": " 1298",
        "new_json": " 1",
        "remoteplace": " txt.yqq.song",
        "searchid": " 66595602585102401",
        "t": " 0",
        "aggr": " 1",
        "cr": " 1",
        "catZhida": " 1",
        "lossless": " 0",
        "flag_qc": " 0",
        "p": str(i),
        "n": " 10",
        "w": keyword,
        "g_tk_new_20200303": " 1390029147",
        "g_tk": " 1390029147",
        "loginUin": " 1164153961",
        "hostUin": " 0",
        "format": " json",
        "inCharset": " utf8",
        "outCharset": " utf-8",
        "notice": " 0",
        "platform": " yqq.json",
        "needNewCode": " 0"
    }


    response = requests.get(url, params=params, headers=headers)
    # print(re.findall('callback\((.*)\)', response)[0])
    search_result = eval(re.findall('callback\((.*)\)', response.text)[0])
    song_info_list = search_result['data']['song']['list']
    # print()
    tplt = "{0:<10}\t{1:<10}\t{2:<20}"
    print(tplt.format("序號", "歌手", "歌名"))
    for song_info in song_info_list:
        # print(song_info)
        song_name = song_info['songname']
        song_mid = song_info['songmid']
        singer = song_info['singer'][0]['name']

找到單個音樂的請求資料介面
音樂的播放地址為purl

動態提交的資料來自與同一個js檔案

除錯js程式碼請求方法為get


get請求的url地址拼接上post對應的表單引數
因為我們獲取的資料只需要資料包含purl的req_1,只需要對應data的req_1的引數,songmid為歌曲的mid

params1 = {
            '_': time.time()*1000,
            'sign': 'zzakgej75pk8w36d82032784bdb9204d99bf6351acb7d',
            "data": '{"req":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"7469768631","songmid":["' + song_mid + '"],"songtype":[0],"uin":"1164153961","loginflag":1,"platform":"20"}}}'
        }
response = requests.get(url1, params=params1, headers=headers)

得到對應的req_1的資料,取出purl值拼接成歌曲播放地址
下載對應歌曲

簡易程式碼分享

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : BaiChuan
# @File    : qq音樂.py

import requests
import re
import time
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
}
keyword = input('請輸入你想下載的歌手名:')

for i in range(1, 10):
    url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
    params = {
        "ct": " 24",
        "qqmusic_ver": " 1298",
        "new_json": " 1",
        "remoteplace": " txt.yqq.song",
        "searchid": " 66595602585102401",
        "t": " 0",
        "aggr": " 1",
        "cr": " 1",
        "catZhida": " 1",
        "lossless": " 0",
        "flag_qc": " 0",
        "p": str(i),
        "n": " 10",
        "w": keyword,
        "g_tk_new_20200303": " 1390029147",
        "g_tk": " 1390029147",
        "loginUin": " 1164153961",
        "hostUin": " 0",
        "format": " json",
        "inCharset": " utf8",
        "outCharset": " utf-8",
        "notice": " 0",
        "platform": " yqq.json",
        "needNewCode": " 0"
    }


    response = requests.get(url, params=params, headers=headers)
    # print(re.findall('callback\((.*)\)', response)[0])
    search_result = eval(re.findall('callback\((.*)\)', response.text)[0])
    song_info_list = search_result['data']['song']['list']
    # print()
    tplt = "{0:<10}\t{1:<10}\t{2:<20}"
    print(tplt.format("序號", "歌手", "歌名"))
    for song_info in song_info_list:
        # print(song_info)
        song_name = song_info['songname']
        song_mid = song_info['songmid']
        singer = song_info['singer'][0]['name']
        print(tplt.format(song_mid, singer, song_name))
        url1 = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
        params1 = {
            '_': time.time()*1000,
            'sign': 'zzakgej75pk8w36d82032784bdb9204d99bf6351acb7d',
            "data": '{"req":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"7469768631","songmid":["' + song_mid + '"],"songtype":[0],"uin":"1164153961","loginflag":1,"platform":"20"}}}'
        }
        response = requests.get(url1, params=params1, headers=headers)
        print(response.json())
        songlink = response.json()['req']['data']['midurlinfo'][0]['purl']
        if songlink == '':
            print('這首歌是付費歌曲無法下載。')
        else:
            path = 'qq音樂/' + song_name + "-" + singer + ".mp3"
            with open(path, 'wb') as f:
                f.write(requests.get('https://isure.stream.qqmusic.qq.com/' + songlink, headers=headers).content)
            print("正在下載:", song_name)

本文章只提供學習,切勿用在其他用途
vip音樂不做講解