码迷,mamicode.com
首页 > 编程语言 > 详细

搜狗音乐爬虫下载python

时间:2018-06-26 22:40:28      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:dex   www   temp   php   .com   %s   ext   sts   pen   

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(rd:\mp3\%s.mp3 % mp3name, "wb") as f:
                        for chunk in r.iter_content(chunk_size=512):
                            if chunk:
                                f.write(chunk)

 

搜狗音乐爬虫下载python

标签:dex   www   temp   php   .com   %s   ext   sts   pen   

原文地址:https://www.cnblogs.com/chif/p/9231433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!