Get all kinds of resources, choose python to write crawler scripts, just crawl what you want and you’re done

We listen to songs or something. Now this restriction and that restriction, either only on this platform, or only on that platform, what a hassle, let's download all the codes directly and play them on the local player, which is great!

insert image description here

1. Preparation

Here we just use python and pycharm. Those who don't have it can install it first.

These two modules also need to be installed

requests 
prettytable 

Open cmd and enter (pip install plus module name) and press Enter and wait, the second is the same.

2. Process ideas

This main implementation

  1. searching feature
  2. Download song function

searching feature

  1. send request to send a request to the previous search function interface
  2. Get data Get all song information data
  3. Parse the data song artist name album song mid (parameters necessary to download songs)
  4. formatted output

Download song function

  1. Splicing the required music url through the acquired song mid
  2. Music url needed to send network request
  3. Get data Get some music links generated in the acquisition (the link where the mp3 data is located)
  4. Send request (the link where the mp3 data is located)
  5. get data music binary data
  6. save data

3. Code part

import requests     # 发送网络请求
import json
import prettytable as pt


headers = {
    
    
    'cookie': 'pgv_pvid=7300130020; tvfe_boss_uuid=242c5295a1cb156d; RK=6izJ0rkfNn; ptcz=622f5bd082de70e3e6e9a077923b48f72600cafd5e4b1e585e5f418570fa30fe; ptui_loginuin=1321228067; luin=o3452264669; o_cookie=3452264669; ts_uid=5501087131; fqm_pvqid=89ea2cc7-6806-4091-989f-5bc2f2cdea5c; lskey=00010000d96bdb23303bc141246a5c2f9a02cf45acf079eaab6645fdc10923e7a0eaac09aa24533ab1299555; fqm_sessionid=d77623f3-4dd0-4709-8e50-a34986b17344; pgv_info=ssid=s4530794666; ts_last=y.qq.com/; ts_refer=ADTAGmyqq; _qpsvr_localtk=0.8458135546904957; euin=oK6kowEAoK4z7eclow6qoiSz7z**; psrf_qqrefresh_token=; psrf_qqaccess_token=; wxuin=1152921504872193707; psrf_qqopenid=; wxrefresh_token=53_FHlMDgGCCfOUCXRxCMGFGYHxwtDV0_rZANu84l_KrYtPYRVSWqpxoWqojdj1coj4CF_H4qNrXw0WrZtQdlsozcwYQbVp0SepI9Jmm_Pfhjk; qqmusic_key=W_X_3mJrl760n0lDWkgqqBhBMX2VPnH1PNwwUZjvNkmSnurNjTTap1J6gjetIU50hraK3AjHgw5T4ay2R3; qm_keyst=W_X_3mJrl760n0lDWkgqqBhBMX2VPnH1PNwwUZjvNkmSnurNjTTap1J6gjetIU50hraK3AjHgw5T4ay2R3; psrf_qqunionid=; tmeLoginType=1; wxunionid=oqFLxsgDOwYRnPDffuK5UINv958c; wxopenid=opCFJw5Vvcf24VLQSyNpKAX6gt3I; wxuin=1152921504872193707; qm_keyst=W_X_3mJrl760n0lDWkgqqBhBMX2VPnH1PNwwUZjvNkmSnurNjTTap1J6gjetIU50hraK3AjHgw5T4ay2R3; login_type=2'
}
name = input('请输入你想要下载的歌曲或者歌手名称:')
# 1. 发送请求 向以前的搜索功能接口发送 请求
url = f'https://c.***/soso/fcgi-bin/client_search_cp?p=1&n=10&w={name}'
response = requests.get(url)
# print(response)
# 2. 获取数据 获取所有歌曲信息数据
json_str = response.text
# 3. 解析数据 歌曲 歌手名 专辑 歌曲mid(用来下载歌曲必须要的参数)
json_str = json_str[9: -1]
json_dict = json.loads(json_str)
song_list = json_dict['data']['song']['list']
tb = pt.PrettyTable()
tb.field_names = ['序号', '歌名', '歌手', '专辑']
music_info_list = []
count = 0
# 4. 格式化输出
for song in song_list:
    songname = song['songname']
    songmid = song['songmid']
    singer = song['singer'][0]['name']
    albumname = song['albumname']
    tb.add_row([count, songname, singer, albumname])
    music_info_list.append([songmid, songname, singer])
    count += 1
print(tb)

while True:
    input_index = eval(input('请输入你要下载的歌曲序号(-1)退出:'))
    if input_index == -1:
        break
    download_info = music_info_list[input_index]
    songmid = download_info[0]
    # 1. 通过获取的歌曲mid 拼接 需要的音乐url
    music_info_url = 'https://u.***/cgi-bin/musicu.fcg?data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch", "filename":"M800","param":{"guid":"8846039534","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","filename":"M800","param":{"guid":"8846039534","songmid":["%s"],"songtype":[0],"uin":"1152921504784213523","loginflag":1,"platform":"20"}},"comm":{"uin":"1152921504784213523","format":"json","ct":24,"cv":0}}' % songmid
    # print(music_info_url)
    # 2. 发送网络请求 需要的音乐url
    json_data = requests.get(url=music_info_url, headers=headers).json()
    # 3. 获取数据 获取 里面生成的 部分音乐链接 合并 (mp3 数据所在的链接了)
    purl = json_data['req_0']['data']['midurlinfo'][0]['purl']
    media_url = 'https://dl.stream.qqmusic.qq.com/' + purl
    # 4. 发送请求 (mp3 数据所在的链接了)
    # 5. 获取数据 音乐二进制数据
    music_data = requests.get(media_url).content
    # 6. 保存数据
    with open(f'歌曲下载/{download_info[1]}-{download_info[2]}.mp3', mode='wb') as f:
        f.write(music_data)
    print(f'{download_info[1]}, 下载完成!')

Brothers, go and try it~

Guess you like

Origin blog.csdn.net/fei347795790/article/details/123237273