a period of cool dog music interface, perfect its VIP music download mode, thinking better pursuit of open source, so write this article, this paper study for reference. Although there is no technical content, but they are their own bit of code, a little bit of scratch.

1, summary:

makes reptile system according to cool dog's search interface and lossless music download interface. Using flask framework, front-end search keyword extraction, the back-end call crawler system to collect data, and data presentation;

operating environment: windows/linux python2.7

two, crawler development:

< p> by way of capturing the cool dog client capture, caught two interfaces:


1 search interface:


? Keyword={keyword keyword transfer through the interface, the return is a JSON data, the data contains music name, artist, album, the total amount of data and other information, of course the most important is the quality of hash data contains all music.

default interface returns data contains only 30 songs, in order to get all the data, only need to put the PageSize to change it, so I extracted the total amount of data, and then launched a data request again, get all of the data. Of course, this is also the total amount of data in JSON total is search results as a basis, if total = = 0 is judged not to search data. Search to

data, I will extract the lossless music hash hash, this is the key of music downloads, lossless Music: SQFileHash, hash key to extract the lossless hash (if it is 32 0 None), I put his name, singer and hash in the form of a dictionary is transferred to the next module.


a. request module (high reuse rate):

 coding=utf-8 import requests import JSON headers # = {'Accept':'text/html, application/xhtml+xml, application/xml; q=0.9, image/webp, image/apng, q=0.8','Accept-Encoding'* / *;:'gzip, deflate','Accept-Language','zh-CN, Zh; q=0.9','Cache-Control':'max-age=0','Connection':'keep-alive','User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko'Chrome/63.0.3239.132 Safari/537.36' def} '), parse (URL): RET = json.loads (requests.get (URL, headers=headers, timeout=5).Text) # return is converted by return RET if __name__ data dictionary: parse ='__main__' (



 coding=utf-8 import # cable module 

copy import MusicParse def search (keyword): search_url ='http://songsearch.kugou.com/song_search_v2? Keyword={}page=1'.format (keyword) to determine whether # here, IP and search fields may limit the search, total judge total = MusicParse.parse (search_url) ['data']['total'] if total! = 0: search_total_url = search_url +'& pagesize=%d'% total music_list = MusicParse.parse (search_total_url) ['data']['lists'] item, items for music in [] = {}, music_list: ='0'*32: item['Song'] = if music['SQFileHash']! Music['SongName'] # song item['Singer'] = music['SingerName'] item['Hash'] = music['SQFileHash'] # singer song # lossless hash Items.append (copy.deepcopy (item)) return items else: return None if __name__'__main__'(

): search =

to this step, music search interface and use finished, below is a lossless music search.


 # music download interface: V2 version of the system, the PC version of Music_api_1 ='http://trackercdnbj.kugou.com/i/v2/? Cmd=23& pid=1& behavior=download'# V2 version of the system, mobile phone edition (standby) Music_api_2 ='http:// trackercdn.kugou.com/i/v2/? Appid=1005& pid=2& cmd=25& behavior=play'; # old version of the system (standby) Music_api_3 ='http://trackercdn.kugou.com/i/? Cmd=4& pid=1& forceDown=0& vip=1'

I am here to prepare the three interface, according to different versions of the cool dog system, using different encryption methods to cool dog music download music key is at the interface of key encryption, key encryption is generated by hash, different cool dog version, different encryption methods: generating a

cool dog V2 version key MD5: (hash + kgcloudv2)

The generation of cool dog old version key: MD5 (hash + kgcloud),

, as long as the hash+key of music is added to api_url, get is submitted to the past, it can return a JSON data, and the data contains music download link, and then extract its download_url. Next, I will use the first interface of cool dog to complete the code implementation. Of course, cool dogs have regional restrictions, and the effectiveness of the interface is also tested. If the first one is not good, then you can know another one. Then I turned the data into a dictionary and passed it.


 coding=utf-8 copy import hashlib # import import MusicParse import MusicSearch # V2 version of the system, PC version, encryption MD5 (hash + kgcloudv2) Music_api_1 ='http://trackercdnbj.kugou.com/i/v2/? Cmd=23& pid=1& amp; behavior=download'# V2 version of the system, mobile phone version, encryption MD5 (hash + "kgcloudv2") (alternate) Music_api_2 ='http://trackercdn.kugou.com/i/v2/? Appid=1005& pid=2& cmd=25& behavior=play' # version of the old system, encryption MD5 (hash + kgcloud) (alternate) Music_api_3 ='http://trackercdn.kugou.com/i/? Cmd=4& pid=1& forceDown=0& vip=1'def V2Md5 (Hash): # used to generate key, suitable for V2 kugou system return hashlib.md5 ((Hash +'kgcloudv2').Encode ('utf-8')).Hexdigest (DEF) Md5 (Hash): for # The old version of the system return hashlib.md5 (Google (Hash +'kgcloud').Encode ('utf-8')).Hexdigest (DEF) HighSearch (keyword): music_list = MusicSearch.search (keyword) if music_list is not None: item items for music in [] = {}, music_list: Hash = str.lower (music['Hash'].encode ('utf-8') key_new) = V2Md5 (Hash) V2 key try: DownUrl # generation system (Music_api_1 = MusicParse.parse +'& hash=%s& key=%s'% (Hash, key_new) ['url']) item['Song'] = music['Song'].encode ('utf-8') # song item['Singer'] = music['Singer'].encode ('utf-8') = DownUrl (items.append # singer item['url'] copy.deepcopy (item)) except KeyError: pass return items if'__main__'(__name__ = = HighSearch) < / The pre> 

cool dog's crawler system is completed, and the flask frame is started to build the front and back ends.


engine, built this search engine is based on the flask framework, the design idea is relatively simple, the front-end post data transfer (keyword) transfer to the rear, rear holding the keyword is passed to the crawler crawler, returns the data to the system, the system in front of the rendered.


 coding=utf-8 sys from flask # import import Flask from flask import request, render_template from KgSpider import HighMusicSearch reload (sys) sys.setdefaultencoding ('utf-8') app = Flask (__name__) @app.route (methods=['GET','POST'] ', DEF) search (if): request.method ='GET': return render_template ('index.html') elif request.method = ='POST': keyword = request.form.get ('keyword') items = HighMusicSearch.HighSearch (keyword) if items! Return render_template = None: ('list.html', list=items) else: return 'could not find!!! Else: return does not support English 'render_template ('404.html') if __name__ = ='__main__': app.run (debug=True) 

four debugging

rectification engine system is designed, so we try:

1. Python run.py

input keyword search

"color: #ff0000" > five

engine, summarizes the completed structures, can run normally, but it is only a model, No consideration. The pressure brought by multi-user access is easy to crash. Of course, after my tests, it was found that it could only search Chinese, and English was totally invalid, why? Don't ask me, I don't know!!! Of course, here I want to say, please respect the copyright!!! Although I am duplicity!!!!!


the above summary is Xiaobian to introduce the Python lossless music search engine code, I hope to help you, if you have any questions please give me a message, Xiao Bian will reply all the time. Thank you for your support for the home website of the script!

This paper fixed link:http://www.script-home.com/python-non-lossless-music-search-engine-implementation-code.html | Script Home | +Copy Link

Article reprint please specify:Python non lossless music search engine implementation code | Script Home

You may also be interested in these articles!