2013.9.20 因为人人改版,可能已无法使用
#-*- coding:UTF-8 -*- ''' ====================================== 此程序根据 http://www.oschina.net/code/snippet_946076_17870 内容改编 Adapted BY: jxy Mail:czjxy8898@gmail.com ====================================== ''' from sgmllib import SGMLParser import sys,urllib2,urllib,cookielib import datetime import time class spider(SGMLParser): def __init__(self,email,password): SGMLParser.__init__(self) self.h3=False self.h3_is_ready=False self.div=False self.h3_and_div=False self.a=False self.depth=0 self.names="" self.dic={} self.email=email self.password=password self.domain='renren.com' try: cookie=cookielib.CookieJar() cookieProc=urllib2.HTTPCookieProcessor(cookie) except: raise else: opener=urllib2.build_opener(cookieProc) urllib2.install_opener(opener) def login(self): print 'start login' url='http://www.renren.com/PLogin.do' postdata={ 'email':self.email, 'password':self.password, 'domain':self.domain } try: req=urllib2.Request( url, urllib.urlencode(postdata) ) self.file=urllib2.urlopen(req).read() idPos = self.file.index("'id':'") self.id=self.file[idPos+6:idPos+15] tokPos=self.file.index("get_check:'") self.tok=self.file[tokPos+11:tokPos+21] rtkPos=self.file.index("get_check_x:'") self.rtk=self.file[rtkPos+13:rtkPos+21] print 'success' return 1 except: print 'error' return 0 def publish(self,content): url1='http://shell.renren.com/'+self.id+'/status' postdata={ 'content':content, 'hostid':self.id, 'requestToken':self.tok, '_rtk':self.rtk, 'channel':'renren', } req1=urllib2.Request( url1, urllib.urlencode(postdata) ) self.file1=urllib2.urlopen(req1).read() print '%s:\n %s have sended\n:(%s)'% (datetime.datetime.now(),self.email,postdata.get('content','')) def tt(T=5): while 1: t = time.localtime() content='24小时测试:现在时间'+str(t.tm_hour)+'时'+str(t.tm_min)+'分'+str(t.tm_sec)+'秒' print content renrenspider.publish(content) time.sleep(60) T=T-1 if T==0: break user=raw_input('email or phone number:') password=raw_input('password:') renrenspider=spider(user,password) if renrenspider.login() : #content=raw_input('please input content:') # renrenspider.publish(content.decode('gb2312').encode('utf-8')) while 1: if time.localtime().tm_sec==0 and time.localtime().tm_min==0: tt(24) break time.sleep(1)