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使用PyGame模块播放声音的方法

本文实例讲述了python使用PyGame模块播放声音的方法。分享给大家供大家参考。具体实现方法如下: import pygame pygame.init() pygame.mixe...

python pytest进阶之xunit fixture详解

前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是set...

浅谈使用Python内置函数getattr实现分发模式

本文研究的主要是使用Python内置函数getattr实现分发模式的相关问题,具体介绍如下。 getattr 常见的使用模式是作为一个分发者。举个例子,如果你有一个程序可以以不同的格式输...

python 地图经纬度转换、纠偏的实例代码

python 地图经纬度转换、纠偏的代码如下所示: # -*- coding: utf-8 -*- import json import urllib import math x_p...

浅谈pytorch和Numpy的区别以及相互转换方法

如下所示: # -*- coding: utf-8 -*- # @Time : 2018/1/17 16:37 # @Author : Zhiwei Zhong # @Site...