谢谢yhylord
自己学习写了python下的抓取爱听fm中坏蛋调频频道的内容,并且用notify但是太粗糙了
贴出来大家一起提意见吧
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import urllib
import urllib2
import sys
import re
import difflib
#####抓取网页内容######
url = "
http://www.itings.com/badfm/usercontent_2590p58543"
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"})
webpage= urllib2.urlopen(req)
websource = webpage.read()
web = open("web.txt",'w')
web.write(websource)
web.close()
p1 = re.compile('audioPath="(.*?mp3)')
p2 = re.compile('proName=(\".*?\")', re.DOTALL)
content1 = p1.findall(websource)
content2 = p2.findall(websource)
####判断source.txt是否已经存在#####
a = os.system("ls source.txt")
if(a == 512):
# print "source.txt不存在"
f1 = open("source.txt",'w')
f4 = open("list.txt", 'w')
for i in content1:
f1.write(i)
f1.write('\n')
f1.close()
for j in content2:
f4.write(j)
f4.write('\n')
f4.close()
else:
# print "source.txt文件已经存在"
f2 = open("newsource.txt", 'w')
f3 = open("newlist.txt", 'w')
for i in content1:
f2.write('%r\n' % (i))
for j in content2:
f3.write(j)
f3.write('\n')
f2.close()
#####比较模块######
def isDiff(srcfile, tarfile):
'''
compare with two files,if equal then return ture
'''
src = file(srcfile).read().split(' ')
tar = file(tarfile).read().split(' ')
ret = 1
# ignore blank lines
temp = difflib.SequenceMatcher(lambda x: len(x.strip()) == 0, src, tar)
for tag, i1, i2, j1, j2 in temp.get_opcodes():
#print tag
if tag != 'equal':
ret = 0
break
return (True if ret == 1 else False)
judge = isDiff("source.txt","newsource.txt")
if (judge == False) :
os.system("notify-send 坏蛋调频没有更新")
else:
os.system("notify-send 坏蛋调频更新了")
print "是否需要调用uget来下载新货?> ",
a=raw_input()
if (a == yes):
os.system("/usr/bin/uget")
else:
print "好吧,俺休息去了"
s1 = open("source.txt", 'w')
s2 = open("newsource.txt")
s3 = s2.read()
s1.write(s3)
s2.close()
s1.close()