云萌主云应用官方论坛
标题: python定时器爬取豆瓣音乐Top榜歌名 [打印本页]
作者: 骑单车的小女孩 时间: 2016-12-30 00:39
标题: python定时器爬取豆瓣音乐Top榜歌名
作者:vpoet
日期:大约在夏季
注:这些小demo都是前段时间为了学python写的,现在贴出来纯粹是为了和大家分享一下
- #coding=utf-8
-
- import urllib
- import urllib2
- import re
- import time
-
-
-
- def SaveTop20Music(currtime):
- rex=r'<a href="javascript:;">(.*?)</a>';
- url = 'http://music.douban.com/chart';
- Response = urllib2.urlopen(url);
- Html=Response.read();
- listsofsong = re.findall(rex, Html);
- print len(listsofsong);
- f=open('%s.txt' % currtime,'w');
- x=1;
- for line in listsofsong:
- f.write('top'+str(x)+':'+line);
- f.write('\n');
- x=x+1;
- f.flush();
-
-
- f.close();
- print currtime+'.txt'+'\t\t'+'SaveOver'
-
-
-
- def timer(n):
- while True:
- currtime = time.strftime("Savetime_%H-%M-%S", time.localtime())
- print currtime
- SaveTop20Music(currtime)
- time.sleep(n)
-
-
-
- if __name__ == "__main__":
- timer(5)
-
复制代码
欢迎光临 云萌主云应用官方论坛 (https://www.yunmengzhu.com/) |
Powered by Discuz! X3.4 |