Skip to content

Instantly share code, notes, and snippets.

@hangchow
Last active September 28, 2015 19:28
Show Gist options
  • Select an option

  • Save hangchow/1485908 to your computer and use it in GitHub Desktop.

Select an option

Save hangchow/1485908 to your computer and use it in GitHub Desktop.
华邮网抢东西脚本
#coding=utf8
import sgmllib,urllib,time
import webbrowser
class MyParser(sgmllib.SGMLParser):
"A simple parser class."
def parse(self, s):
"Parse the given string 's'."
self.feed(s)
self.close()
def __init__(self, verbose=0):
"Initialise"
sgmllib.SGMLParser.__init__(self, verbose)
self.hyperlinks = []
self.images = []
self.title=''
self.forumstats = ''
self.inside_p = False
self.inside_title=False
self.contents=[]
def start_title(self, attrs):
self.inside_title = True
def end_title(self):
self.inside_title = False
def handle_data(self, data):
if self.inside_title and data:
self.title = self.title + data + ' '
if self.inside_p and data:
data = uni(data)
self.forumstats += data
def start_p(self, attrs):
for name, value in attrs:
if name == "class" and value == "forumstats":
self.inside_p = True
def end_p(self):
self.inside_p = False
def start_meta(self, attributes):
for name, value in attributes:
if name == "content":
self.contents.append(value)
def get_hyperlinks(self):
return self.hyperlinks
def get_images(self):
return self.images
def get_title(self):
return self.title.strip()
def get_countent(self):
return self.contents
def get_forumstats(self):
return self.forumstats
def uni(txt):
return txt.decode('gbk').encode('utf8')
url = "http://www.p1878.com/bbs/forumdisplay.php?fid=210"
i = 0;
zhuti = 0;
huifu = 0;
while True:
try:
f = urllib.urlopen(url)
s = f.read()
f.close()
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
continue
myparser = MyParser()
myparser.parse(s)
#output
print "============================================="
i=i+1
print "times:" + str(i)
print "url:" + url
print "title:" + uni(myparser.get_title())
#countents = myparser.get_countent()
#for countent in countents:
# print "meta:" + uni(countent)
stats = myparser.forumstats
print "stats:" + stats
statarray = stats.split(' ')
newzhuti = statarray[1]
newhuifu = statarray[4]
print "zhuti:" + newzhuti
print "huifu:" + newhuifu
if (zhuti != 0) and (newzhuti > zhuti):
print 'has new zhuti'
webbrowser.open(url)
if (huifu != 0) and (newhuifu > huifu):
print 'has new huifu'
zhuti = newzhuti
huifu = newhuifu
time.sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment