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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

python 中的列表生成式、生成器表达式、模块导入

5.16 列表生成式 l=[] for i in range(100): l.append('egg%s' %i) print(l) ​ l=['egg%s' %i...

Python中的集合介绍

Python中的集合介绍

1.集合的定义 集合的元素是不可重复的 s = {1,2,3,1,2,3,4,5} print(s) print(type(s)) s1 = {1} print(s1) print(...

python赋值操作方法分享

一、序列赋值: x,y,z = 1,2,3 我们可以看作:x = 1,y = 2,z = 3 二、链接赋值: x = y = 1print id(x)print id(y) 大家可以看下...

Django REST framework 如何实现内置访问频率控制

对匿名用户采用 IP 控制访问频率,对登录用户采用 用户名 控制访问频率。 from rest_framework.throttling import SimpleRateThrot...

python实现两个字典合并,两个list合并

1.两个字典:a={‘a':1,'b':2,'c':3} b= {‘aa':11,'bb':22,'cc':33} 合并1:dict(a,**b) 操作如下: >>>...