Home >Backend Development >Python Tutorial >python基于urllib实现按照百度音乐分类下载mp3的方法

python基于urllib实现按照百度音乐分类下载mp3的方法

WBOY
WBOYOriginal
2016-06-10 15:11:361399browse

本文实例讲述了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('>.*<.a', s1, re.M).group()
    s3 = s2[1:-3]
    urllib.urlretrieve(down, s3+".mp3")

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn