閱讀875 返回首頁    go 阿裏雲 go 技術社區[雲棲]


python 實現 自動定時發送人人狀態

2013.9.20  因為人人改版,可能已無法使用


#-*- coding:UTF-8 -*-
'''
======================================
此程序根據 https://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='https://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='https://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)


最後更新:2017-04-04 07:03:51

  上一篇:go jQuery對select的基本操作
  下一篇:go GridView的點擊事件不能被監聽