本文实例讲述了python基于urllib实现按照百度音乐分类下载mp3的方法。分享给大家供大家参考。具体实现方法如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import urllib
import re
baseurl = “http://music.baidu.com”
url = “http://music.baidu.com/search/tag?key=经典流行”
html = urllib.urlopen(url).read()
uri = re.findall(r’/song/\d+’, html, re.m)
lst = []
for i in uri:
link = baseurl+i+”/download”
lst.insert(0, link)
for k in lst:
res = urllib.urlopen(k).read()
down = re.search(‘http://[^ ]*xcode.[a-z0-9]*’ , res, re.m).group()
s1 = re.search(‘title=”.*’,res, re.m).group()
s2 = re.search(‘>.*