Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to climb Cool my Music with Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Python to climb cool my music, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Two days ago, I listened to the music on the official website of Cool my Music. I felt that the music above was good, and I wanted to climb them. At first, I didn't have a clue at all, but in the end, I had a way to implement it, that is, I finally got the music I wanted to hear through two json files.

Insert the Python module needed for the picture description here

The main modules to implement this process are requests, json, urllib.request and urllib.parse, in which the requests module is used to request the corresponding data (in this case, the json data is obtained), and the json module is used to process the obtained json data (converting the json data into a dictionary, mainly using the json.loads () method), urllib.request (using its urlretrieve () method for downloading music), urllib.parse (using its quote () method Used to encode the input string.

Realization idea

First of all, we need to go to the official website of Kuwi Music (http://www.kuwo.cn/)) and enter the keywords in the input box. What the editor enters is: break the cocoon and enter, and you can get the corresponding song list. However, these data are loaded dynamically. If you use the requests module to request this URL directly, it is impossible to get the data. At this time, we can press the computer keyboard F12. When you come to developer mode, click XHR under Network to find the URL of these song lists, specifically:

We need to get the corresponding data in these songs as follows:

The values corresponding to the name and artist keywords are the presentation and final .mp3 file names, and the values corresponding to the rid keyword are used by later procedures.

Of course, it is not easy to access this URL, and you need to add a request header. The reference code is as follows:

MusicName=input ('Please enter the song name:')

EncodName=quote (musicName)

Url=' https://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn=1&rn=30&httpsStatus=1'.format(encodName)

Referer=' https://www.kuwo.cn/search/list?key={}'.format(encodName)

# request header

Headers = {

"Cookie": "_ ga=GA1.2.2021007609.1602479334; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1602479334,1602673632; _ gid=GA1.2.168402150.1602673633; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1602673824; kw_token=5LER5W4ZD1C"

"csrf": "5LER5W4ZD1C"

"Referer": "{}" .format (referer)

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"

}

Response=requests.get (url=url,headers=headers)

Dict2=json.loads (response.text)

MisicInfo=dict2 ['data'] [' list'] # list of song information

MusicNames=list () # list of song names

Rids=list () # stores a list of songs rid

For i in range (len (misicInfo)):

Name=misicInfo [I] ['name'] +' -'+ misicInfo [I] ['artist']

MusicNames.append (name)

Rids.append (misicInfo [I] ['rid'])

Print ('[{}]-{}-> > {} '.format (random.random () * 10) *' # $', name))

When we choose the songs in the above list to listen, we can find that there is a URL like this at the bottom of the list just now, and there is also a json data in it, which contains the download link of the song I tried to listen to. As follows:

By analyzing this URL, we can see that we need the rid data just now to access the corresponding json data. The URL is: http://www.kuwo.cn/url?format=mp3&rid=**140897945**&response=url&type=convert\_url3&br=128kmp3&from=web&t=1603463521198&httpsStatus=1, maybe the length of the URL obtained by the reader is a little longer than mine. I have removed the last parameter, because I found that without the last parameter, I can still access the corresponding data.

Final code and run result

The reference code is as follows:

From urllib.request import urlretrieve

From urllib.parse import quote

Import requests

Import random

Import json

MusicName=input ('Please enter the song name:')

EncodName=quote (musicName)

Url=' https://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn=1&rn=30&httpsStatus=1'.format(encodName)

Referer=' https://www.kuwo.cn/search/list?key={}'.format(encodName)

# request header

Headers = {

"Cookie": "_ ga=GA1.2.2021007609.1602479334; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1602479334,1602673632; _ gid=GA1.2.168402150.1602673633; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1602673824; kw_token=5LER5W4ZD1C"

"csrf": "5LER5W4ZD1C"

"Referer": "{}" .format (referer)

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"

}

Response=requests.get (url=url,headers=headers)

Dict2=json.loads (response.text)

MisicInfo=dict2 ['data'] [' list'] # list of song information

MusicNames=list () # list of song names

Rids=list () # stores a list of songs rid

For i in range (len (misicInfo)):

Name=misicInfo [I] ['name'] +' -'+ misicInfo [I] ['artist']

MusicNames.append (name)

Rids.append (misicInfo [I] ['rid'])

Print ('[{}]-{}-> > {} '.format (random.random () * 10) *' # $', name))

Id=int (input ('Please enter the song serial number:')

MusicRid= rids [id-1]

Url2=' https://www.kuwo.cn/url?format=mp3&rid={}&response=url&type=convert_url3&br=128kmp3&from=web&t=1602674521838&httpsStatus=1'.format(musicRid)

Headers2= {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"}

Response2=requests.get (url=url2,headers=headers2)

Dict3=json.loads (response2.text)

DownloadUrl=dict3 ['url']

Path=input ('Please enter storage path:')

Urlretrieve (url=downloadUrl,filename=path+'\ {} .mp3 '.format (musicNams [id-1])) # download the song

Running result:

Find the appropriate directory and find an extra .mp3 file under this folder.

Summary

First of all, the editor first declares that the reference code of this program is for learning only and must not be used for commercial activities. Once found by the relevant personnel, this editor is not responsible!

In addition, it should be pointed out that readers are expected not to run the program code many times a day, so as to reduce the burden of services.

There may be some deficiencies in the program code! There is no detailed analysis of the parameters in these two sites, readers can try if they are interested. If the reader thinks the editor's article is not bad! Don't forget to order a little like when you leave!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report