写个Python脚本每天自动下载VOA录音

学习英文练听力就要多听。VOA Special English对我这种听力不好的同志很适合,所以我常去网站下载,不过时间长了就觉得很不方便。干脆写个Python脚本,再加到Windows的“计划任务”里就可以每天自动为我下载了。:)

 

Python脚本很直接简单 ,用到都是最基本的Python库,直接把代码贴上来。

import urllib2 import os import datetime import string import re import unittest VOA_URL=r'http://www.unsv.com/' def getFileNameFromLink(strLink): filename_re=r'[^/:]+.mp3' matched=re.search(filename_re, strLink) if matched: return matched.group(0) else: return r'' def genMp3DownLinkRe(date): return r'http[^"]*'+date+r'[^"]*/.mp3' def genDateStr(rawStr): return string.replace(rawStr, r'-', r'') def getMp3Links(aRe): print r'Reading VOA page...', response=urllib2.urlopen(VOA_URL) html=response.read() print r'Done.' print r'Trying to parse the page and get the right mp3 links...', matchedLinks=re.findall(aRe, html) print r'Done.' return matchedLinks if __name__ == '__main__': #Get today's data string str_today=genDateStr(str(datetime.date.today())) if not os.path.isdir(str_today): print r"Can't find the date directory, create one...", os.mkdir(str_today) print r'Done' link_re=genMp3DownLinkRe(str_today) mp3Links=getMp3Links(link_re) mp3LinksSet=set() for link in mp3Links: mp3LinksSet.add(link) print r'Found %d link, begin to download them...'%len(mp3LinksSet) for link in mp3LinksSet: print r'Handling link %s...'%link filename=getFileNameFromLink(link) if not filename==r'': wholename=os.path.join(str_today, filename) if os.path.isfile(wholename): print '/tThe file has been downloaded. Skip this file.' continue print '/tDownloading the mp3 file...', data=urllib2.urlopen(link).read() print 'Done' print '/tWriting data info file...', f=file(wholename, 'wb') f.write(data) print 'Done' f.close() print 'All Finished. ^_^'

 

方便多了,程序改变人们的生活啊!

你可能感兴趣的:(杂记)