搜狗音乐爬虫下载python
Posted chif
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搜狗音乐爬虫下载python相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章