Download songs on cool dog music

After testing, you can download the songs you want to pay to download

 

Preparation: Python 3.5 + pycharm

Libraries used: requests, re, json

 

Steps:

Open the official website of cool dog music, enter the songs you want to search (such as "Tian Tian"), and then press enter to search to get the following page:

 

Right click to check or press F12 to open the developer tool, click JS option and find the following information. FileName is the returned singer name and song name information, which we want to extract:

Since the web address does not return the standard json format, the following processing should be carried out first, and then it should be converted to the json format:

res = requests.get(url).text
js = json.loads(res[res.index('(') + 1:-2])
After extracting the song information, we input a number to represent the number of songs to download, and then download the songs.



Running screenshot:

 

Attach source code:
 1 import requests
 2 import json
 3 import re
 4 
 5 
 6 def get_song(x):
 7     url = "http://songsearch.kugou.com/song_search_v2?callback=jQuery112407470964083509348_1534929985284&keyword={}&" \
 8           "page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filte" \
 9           "r=0&_=1534929985286".format(x)
10     res = requests.get(url).text
11     js = json.loads(res[res.index('(') + 1:-2])
12     data = js['data']['lists']
13     for i in range(10):
14         print(str(i + 1) + ">>>" + str(data[i]['FileName']).replace('<em>', '').replace('</em>', ''))
15     number = int(input("\n Please enter the song number to download (enter-1 Exit program): "))
16     if number == -1:
17         exit()
18     else:
19         name = str(data[number - 1]['FileName']).replace('<em>', '').replace('</em>', '')
20         fhash = re.findall('"FileHash":"(.*?)"', res)[number - 1]
21         hash_url = "http://www.kugou.com/yy/index.php?r=play/getdata&hash=" + fhash
22         hash_content = requests.get(hash_url)
23         play_url = ''.join(re.findall('"play_url":"(.*?)"', hash_content.text))
24         real_download_url = play_url.replace("\\", "")
25         with open(name + ".mp3", "wb")as fp:
26             fp.write(requests.get(real_download_url).content)
27         print("The song has been downloaded!")
28 
29 
30 if __name__ == '__main__':
31     x = input("Please enter a song name:")
32     get_song(x)

Keywords: Python JSON Pycharm PHP

Added by ultrus on Sun, 05 Jan 2020 01:21:51 +0200