云萌主云应用官方论坛

标题: python定时器爬取豆瓣音乐Top榜歌名 [打印本页]

作者: 骑单车的小女孩    时间: 2016-12-30 00:39
标题: python定时器爬取豆瓣音乐Top榜歌名
作者:vpoet
日期:大约在夏季
注:这些小demo都是前段时间为了学python写的,现在贴出来纯粹是为了和大家分享一下
来源:http://www.open-open.com/code/view/1435213558857
  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('\n');  
  22.         x=x+1;  
  23.         f.flush();  
  24.          
  25.          
  26.     f.close();  
  27.     print currtime+'.txt'+'\t\t'+'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)  
  42.    
复制代码







欢迎光临 云萌主云应用官方论坛 (https://www.yunmengzhu.com/) Powered by Discuz! X3.4