分页: 1 / 1

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

发表于 : 2013-11-29 10:42
Lavande
用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!')

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

发表于 : 2013-11-30 13:40
hoxily
首先,你这个程序的输入只有一条,那就是专辑主页URL.
然后,你这个程序的输出只是一些处理过程的log记录.
所以图形界面的话,得有一个输入URL的文本框,一个显示输出log的文本框,还得有一个触发下载操作的按钮.这3个元素大概够用了.
:em06
附上我的C# 4.0 豆瓣音乐下载工具, 从你的python代码移植而来.希望有帮助.
至于mac, :em06

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

发表于 : 2013-11-30 13:44
ceclinux
:em20 嵌套了3个for

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

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