This article describes how to use Python to crawl the music url in qqmusic and batch download information. The article provides a detailed introduction and sample code, I believe it has some reference value for everyone. let's take a look at it. This article mainly introduces how to use # wiki/1514.html "target =" _ blank "> Python to crawl music URLs in qq music and related materials for batch download, this article provides a detailed introduction and sample code. I believe it will be of reference value to everyone. let's take a look at it.
Preface
There are still a lot of music on qq music, and sometimes you want to download nice music, but every time you download it on the web page, it is annoying to log on or something. So I came to a QQ music crawler. At least I think the core of for loop crawler is to find the url of the element to be crawled. Let's start looking for it. (don't laugh at me if you're not talking about it)
The implementation is as follows:
# Search for URLs:
This url is not as easy to find as other websites. I am tired, but the key is that there is a lot of data. I pick out useful data from that data and combine it into music. Several intermediate URLs sorted during yesterday's work:
# Url1: https://c.y.qq.com/soso/fcgi-bin/client_search_cp? & Lossless = 0 & flag_qc = 0 & p = 1 & n = 20 & w = Yudie
# Url2: https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg? & JsonpCallback = MusicJsonCallback & cid = 205361747 & [songmid] & C400 + songmid +. m4a & guid = 6612300644
# Url3: http://dl.stream.qqmusic.qq.com/?filename]? Vkey = [vkey] (where vkey replaces the string unique to music)
requests(url1)
The songmid and mid values of each music are obtained from the search list. (the two values are unique to each music ). With these two values. The complete url2 value is obtained below.
requests(url2)
The vkey value of each music in the search result is obtained. after my observation, the filename is C400songmid. m4a. Then the specific url3 value is determined. Url3 is the real url of music. because the author does not thoroughly research other url parameters, a maximum of 20 music URLs can be returned each time, then the music of Tencent can be enjoyed.
# Code
The following code block of srcs:
Import requestsimport urllibimport jsonword = 'yudie 'res1 = requests. get ('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 = '+ word) jm1 = json. loads (res1.text. strip ('callback () [] ') jm1 = jm1 ['data'] ['song'] ['list'] mids = [] songmids = [] srcs = [] songnames = [] singers = [] for j in jm1: try: mids. append (j ['media _ mid ']) songmids. append (j ['songmid']) songnames. append (j ['songname']) singers. append (j ['singer'] [0] ['name']) handle T: print ('wrong ') for n in range (0, len (mids ): Res2 = requests. get ('https: // c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg? & JsonpCallback = MusicJsonCallback & cid = 205361747 & songmid = '+ songmids [n] +' & filename = C400 '+ mids [n] + '. m4a & guid = 6612300644 ') jm2 = json. loads (res2.text) vkey = jm2 ['data'] ['items '] [0] ['vkey'] srcs. append ('http: // dl.stream.qqmusic.qq.com/c400'?mids=n={'.m4a? Vkey = '+ vkey +' & guid = 6612300644 & uin = 0 & fromtag = 66 ')
# Download:
With srcs, downloading is naturally not a problem. Of course, you can copy src to your browser to download the artist and song name. You can also use large Python for batch download, which is nothing more than a loop. it is similar to the method for downloading sogou images: (my py version: python3.3.3)
print('For '+word+' Start download...') x = len(srcs)for m in range(0,x): print(str(m)+'***** '+songnames[m]+' - '+singers[m]+'.m4a *****'+' Downloading...') try: urllib.request.urlretrieve(srcs[m],'d:/music/'+songnames[m]+' - '+singers[m]+'.m4a') except: x = x - 1 print('Download wrong~')print('For ['+word+'] Download complete '+str(x)+'files !')
The above two pieces of code are written in the same py file, and you can download the music of the corresponding keyword after you run it.
# Running effect:
Music has been downloaded successfully...
So far, the url crawler idea and implementation of qqmusic have been described.
# Purpose:
Musicplayer should be used as a shell. In fact, the original intention is to serve my html-based musicplayer. But now it's stuck in the js call of py. let me look for it again. I 'd like to tell you, thank you very much!
The above is the detailed content for Python to crawl the music url in qq music and batch download methods. For more information, see other related articles in the first PHP community!