搜狗音樂爬蟲下載python
阿新 • • 發佈:2018-06-27
爬蟲 AC active ref ext json color pattern nbsp
import requests import re session = requests.Session() r = session.get(‘http://www.kugou.com/yy/rank/home/1-8888.html?from=homepage‘) html = r.text pattern = r‘<a href="(.+?)" data-active="playDwn" data-index="\d+" class="pc_temp_songname" title="(.+?)" hidefocus="true">.+?</a>‘ m = re.findall(pattern, html)if m: for line in m: # print line mp3name = line[1] r = session.get(line[0]) html = r.text m = re.search(r‘\[\{"hash":"(.+?)".+"album_id":(\d*)\}\]‘, html) if m: 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() if d["status"] == 1: mp3url = d["data"]["play_url"] r = session.get(mp3url, stream=True) with open(r‘d:\mp3\%s.mp3‘ % mp3name, "wb") as f:for chunk in r.iter_content(chunk_size=512): if chunk: f.write(chunk)
搜狗音樂爬蟲下載python