python定时器爬取豆瓣音乐Top榜歌名
作者:vpoet
mail:vpoet_sir@163.com
注:这些小demo都是前段时间为了学python写的,现在贴出来纯粹是为了和大家分享一下
1 #coding=utf-8 2 3 import urllib 4 import urllib2 5 import re 6 import time 7 8 9 10 def SaveTop20Music(currtime): 11 rex=r'<a href="javascript:;">(.*?)</a>'; 12 url = 'http://music.douban.com/chart'; 13 Response = urllib2.urlopen(url); 14 Html=Response.read(); 15 listsofsong = re.findall(rex, Html); 16 print len(listsofsong); 17 f=open('%s.txt' % currtime,'w'); 18 x=1; 19 for line in listsofsong: 20 f.write('top'+str(x)+':'+line); 21 f.write(' '); 22 x=x+1; 23 f.flush(); 24 25 26 f.close(); 27 print currtime+'.txt'+' '+'SaveOver' 28 29 30 31 def timer(n): 32 while True: 33 currtime = time.strftime("Savetime_%H-%M-%S", time.localtime()) 34 print currtime 35 SaveTop20Music(currtime) 36 time.sleep(n) 37 38 39 40 if __name__ == "__main__": 41 timer(5)
运行截图: