python下载音乐文件

需要下载安装 BeautifulSoup,下载地址: beautifulsoup4-4.3.2.tar.gz

下载完成后解压:进入目录打开cmd,执行以下两个操作即可完成安装:

(1)python setup.py build 

(2)python setup.py install 


源代码:

#!/usr/bin/env python  
#-*- coding: UTF-8 -*-  
  
import sys,os  
import urllib,urllib2  
from bs4 import BeautifulSoup  
import json  
from multiprocessing import Process  
  
class BaiDuMusic():  
    def __init__(self):  
        reload(sys)    
        sys.setdefaultencoding('utf8')     
  
    def search(self,songName):  
        firstUrl = "http://music.baidu.com/search?key="+urllib.quote(str(songName))  
        userAgent = " User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 "  
        headers = { 'User-Agent' : userAgent }  
        requst = urllib2.Request(firstUrl,headers = headers)   
        result = urllib2.urlopen(requst).read()  
  
        #使用BeautifulSoup快速解析html文档  
        soup = BeautifulSoup(result,from_encoding="utf-8")  
        res_arr = []  
        try:  
            tmpjson = soup.find_all("li", { "class" : "bb-dotimg clearfix song-item-hook " })  
            for x in tmpjson:  
                tmpobj = json.loads(x['data-songitem'])  
                value = unicode(tmpobj['songItem']['oid'])+"+++"+unicode(tmpobj['songItem']['author'])+"+++"+unicode(tmpobj['songItem']['sname'])[4:-5]  
                res_arr.append(value)  
            return res_arr  
        except Exception, e:  
            print u"抱歉没有找到相关资源".encode("utf-8")  
            return 0  
    def download(self,songid,songName,savePath="down/"):  
        songNewUrl = "http://music.baidu.com/data/music/file?link=&song_id="+str(songid)  
        if not os.path.isdir(savePath):   
            os.makedirs(savePath)  
        savemp3 = savePath.decode('utf-8')+songName.decode('utf-8')+u".mp3"  
        urllib.urlretrieve(songNewUrl, savemp3)   
   
if __name__=='__main__':  
  
    bMusic = BaiDuMusic()  
    res = bMusic.search(u"冰雨")  
    # for x in res:   
        # print x  
    # 1128053+++刘德华+++冰雨  
    # 7327899+++李翊君+++冰雨  
    # 53535187+++张恒+++冰雨  
    Process(target=bMusic.download, args=(1128053,"刘德华-冰雨")).start()  
    Process(target=bMusic.download, args=(7327899,"李翊君-冰雨")).start()  
    Process(target=bMusic.download, args=(53535187,"张恒-冰雨")).start()  


下载文件有以下方法:


方法一:

 urllib.urlretrieve(url, savepath)  <span style="font-family: Arial, Helvetica, sans-serif;">#url:源文件地址 savepath:下载保存路径</span>

代码示例:

import urllib

urllib.urlretrieve('http://music.baidu.com/data/music/file?link=&song_id=1128053', 'C:/BingYu.mp3')  


方法二:

f = urllib2.urlopen(url)  #url:源文件地址
data = f.read()  
with open(savepath, 'wb') as code:  #savepath:下载保存路径
    code.write(data) 
代码示例:

import urllib2

f = urllib2.urlopen('http://music.baidu.com/data/music/file?link=&song_id=1128053')  
data = f.read()  
with open('C:/BingYus.mp3', 'wb') as code:  
    code.write(data)

方法三:

#!/usr/bin/env python  
#-*- coding: UTF-8 -*-  
import requests

req = requests.get('http://music.baidu.com/data/music/file?link=&song_id=1128053'.decode('utf-8'))  
with open('C:/冰雨.mp3'.decode('utf-8'), 'wb') as code:  
    code.write(req.content) 

注:参考自: http://blog.csdn.net/ithomer/article/details/13999845

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值