用Python批量下载豆瓣小站的音乐

软件和网站开发以及相关技术探讨
回复
头像
Lavande
论坛版主
帖子: 5353
注册时间: 2008-12-21 15:27
来自: TARDIS

用Python批量下载豆瓣小站的音乐

#1

帖子 Lavande » 2013-11-29 10:42

用python3写的,妈的,2和3确实是两种不同的语言……
我想把这个小程序弄成GUI的,之前折腾过一阵子,发现好费事,要学好多东西
虽然这个自己用已经够了,给有基础的同学用也够了
但是如果发给win和mac的同学,他们摸不着头脑的
有朋友愿意做一个简单的界面不?

代码: 全选

import urllib.request
from urllib.request import urlopen
import re,os

url = input('Please enter the URL of the douban site.\n(E.g., http://site.douban.com/quinta-diminuita/)\n')
content = urlopen(url).read().decode('utf-8')
#分析音乐人名称并创建相应目录
site_name = ''.join(re.findall('<title>\n\s*(.*?)</title>', content, re.DOTALL))
artist = site_name.replace('的小站\n(豆瓣音乐人)','')
print('Preparing to download all the songs by '+artist+'...')
if os.path.exists('./'+artist) == False:
	os.mkdir('./'+artist)
#分析出所有房间地址
roomlist = re.findall('<div class=\"nav-items\">(.*?)<\/div>', content, re.DOTALL)
roomurl = re.findall('href=\"(.*?)\"', roomlist[0])
#对于每个房间,如果有播放列表,则分析每个列表中的歌曲名和对应地址,然后下载
for h in roomurl:
	content = urlopen(h).read().decode('utf-8')
	playlist = re.findall('song_records\s=\s\[(.*?)\]\;\n', content, re.DOTALL)
	if playlist != []:
		for i in playlist:
			playlist_spl = re.split(r'},{', i)
			for j in playlist_spl:
				song_title = ''.join(re.findall('\"name\"\:\"(.*?)\"', j))
				song_url = re.findall('\"rawUrl\"\:\"(.*?)\"', j)
				song_url_str = ''.join(song_url).replace('\\','')
				filepath = './'+artist+'/'+song_title+'.mp3'
				if os.path.isfile(filepath) == False:
					print('Downloading '+song_title+'...')
					urllib.request.urlretrieve(song_url_str, filepath)
				else:
					print(song_title+' alreadly exists. Skipped.')
print('All the songs have been downloaded (if there are any). Enjoy!')
头像
hoxily
帖子: 39
注册时间: 2011-02-11 21:10

Re: 用Python批量下载豆瓣小站的音乐

#2

帖子 hoxily » 2013-11-30 13:40

首先,你这个程序的输入只有一条,那就是专辑主页URL.
然后,你这个程序的输出只是一些处理过程的log记录.
所以图形界面的话,得有一个输入URL的文本框,一个显示输出log的文本框,还得有一个触发下载操作的按钮.这3个元素大概够用了.
:em06
附上我的C# 4.0 豆瓣音乐下载工具, 从你的python代码移植而来.希望有帮助.
至于mac, :em06
附件
DoubanMusicDownload.7z
C# 4.0 豆瓣音乐下载工具, 从你的python代码移植而来.希望有帮助.
(18.09 KiB) 已下载 87 次
上次由 hoxily 在 2013-11-30 18:45,总共编辑 1 次。
头像
ceclinux
帖子: 308
注册时间: 2013-01-17 2:42
系统: Ubuntu 12.04LTS

Re: 用Python批量下载豆瓣小站的音乐

#3

帖子 ceclinux » 2013-11-30 13:44

:em20 嵌套了3个for
Ubuntu 12.04 LTS
laptop:Acer 4750G CPU:I5-2410
内存:2+2G 硬盘:500+750
http://www.ceclinux.org
头像
Lavande
论坛版主
帖子: 5353
注册时间: 2008-12-21 15:27
来自: TARDIS

Re: 用Python批量下载豆瓣小站的音乐

#4

帖子 Lavande » 2013-11-30 22:13

hoxily 写了:首先,你这个程序的输入只有一条,那就是专辑主页URL.
然后,你这个程序的输出只是一些处理过程的log记录.
所以图形界面的话,得有一个输入URL的文本框,一个显示输出log的文本框,还得有一个触发下载操作的按钮.这3个元素大概够用了.
:em06
附上我的C# 4.0 豆瓣音乐下载工具, 从你的python代码移植而来.希望有帮助.
至于mac, :em06
其实我本来打算GUI再加一些功能的,比如先列出曲目,然后勾选(不)想要下载的。。。 :em01
回复