python下载喜马拉雅音频

#coding=utf-8

import urllib
import urllib2
from bs4 import BeautifulSoup
import json

#音频集网址
req = urllib2.Request('http://www.ximalaya.com/17322657/album/414595/')
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
response = urllib2.urlopen(req)
html = response.read()
soup = BeautifulSoup(html, 'html.parser', from_encoding='utf-8')
divs = soup.select('.personal_body')
for div in divs:
  ids = div.attrs['sound_ids'].split(',')
  for id in ids:
    item_url = 'http://www.ximalaya.com/tracks/' + id + '.json'
    item_req = urllib2.Request(item_url)
    item_req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
    item_response = urllib2.urlopen(item_req)
    item_json = item_response.read()
    parsed_json = json.loads(item_json)
    title = parsed_json['title']
    music = parsed_json['play_path_64']
    print title
    print music
    #下载到本地
    urllib.urlretrieve(music, "dl\\" + title + '.m4a')

你可能感兴趣的:(python下载喜马拉雅音频)