ImportRequestsImportresession=requests. Session () R= Session.get ('Http://www.kugou.com/yy/rank/home/1-8888.html?from=homepage') HTML=R.textpattern= R'<a href= "(. +?)" data-active= "Playdwn" data-index= "\d+" class= "Pc_temp_songname" title= "(. +?)" hidefocus= "true ">.+?</a>'m=Re.findall (pattern, HTML)ifm: forLineinchm:#Print LineMp3name = line[1] R=Session.get (line[0]) HTML=R.text M= Re.search (r'\[\{"Hash": "(. +?)". + "album_id":(\d*) \}\]', HTML)ifm:hash,album_id= M.group (1), M.group (2) URL='http://www.kugou.com/yy/index.php?r=play/getdata&hash=%s&album_id=%s&_=1508983920130'%(hash, album_id)Print(URL) r=session.get (URL) d=R.json ()ifd["Status"] = = 1: Mp3url= d["Data"]["Play_url"] R= Session.get (Mp3url, stream=True) with open (R'D:\mp3\%s.mp3'% Mp3name,"WB") as F: forChunkinchR.iter_content (chunk_size=512): ifChunk:f.write (Chunk)
Sogou music crawler Download Python