使用python在校內發人人網狀態(人人網看狀態)_python

來源:互聯網
上載者:User

複製代碼 代碼如下:

#_*_coding:utf_8_

from sgmllib import SGMLParser
import sys, urllib2, urllib, cookielib
import datetime, time

class spider(SGMLParser):

    def __init__(self, email, password):
        SGMLParser.__init__(self)

        self.email = email
        self.password = password
        self.domain = 'renren.com'

        try:
            cookie = cookielib.CookieJar()
            # a class to handle HTTP cookies
            cookieProc = urllib2.HTTPCookieProcessor(cookie)
        except:
            raise
        else:
            opener = urllib2.build_opener(cookieProc)
            urllib2.install_opener(opener)      

    def login(self):
        print '開始登入'
        url = 'http://www.renren.com/PLogin.do'
        #url = 'http://www.renren.com/SysHome.do'
        postdata = {
                    'email': self.email,
                    'password': self.password,
                    'domain': self.domain 
                   }
        # 一般情況下引入urllib2的地方都需要引入urllib,因為需要urlencode()
        req = urllib2.Request(
                              url,
                              urllib.urlencode(postdata)           
                             )

        self.file = urllib2.urlopen(req).read()
        # urlopen後 成功後進入首頁 因此self.file的內容就是首頁的html檔案的內容
        # print self.file

        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]

    def publish(self, content):
        url1 = 'http://shell.renren.com/' +self.id+ '/status'
        print 'self.id = ' , self.id
        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 datetime.datetime.now()
        print '剛才帳號 %s發了一條狀態' % self.email 
        print '內容為: %s' % postdata.get('content', '')

renrenspider = spider('qich555550@163.com', 'qishibo123')
renrenspider.login()
#content = raw_input('請輸入狀態的內容:')
contents =["祝","各","位","同","學","盆","友","在","新","的","一","年","裡","身","體","健","康","萬","事","如","意","不小心刷屏了,望大家諒解"]
#renrenspider.publish(content)
#content = "新年快樂"
#renrenspider.publish(content)
#renrenspider.publish(content.decode('gb2312').encode('utf-8'))

for content in contents:
    renrenspider.publish(content)

用這個程式就可以髮狀態刷屏了,只不過校內的狀態不支援具體時間,看不出效果來,不然每隔兩秒一條狀態應該會讓人驚訝的 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.