python发人人状态

python发人人状态

 1 #-*- coding:UTF-8 -*-
 2 '''
 3 ======================================
 4 此程序根据 http://www.oschina.net/code/snippet_148170_10661 内容改编
 5 Adapted BY: bepcao
 6 Mail:peterc9511@gmail.com
 7 ======================================
 8 '''
 9 from sgmllib import SGMLParser
10 import sys,urllib2,urllib,cookielib
11 import datetime
12 import time
13 class spider(SGMLParser):
14     def __init__(self,email,password):
15         SGMLParser.__init__(self)
16         self.h3=False
17         self.h3_is_ready=False
18         self.div=False
19         self.h3_and_div=False
20         self.a=False
21         self.depth=0
22         self.names=""
23         self.dic={}   
24          
25         self.email=email
26         self.password=password
27         self.domain='renren.com'
28         try:
29             cookie=cookielib.CookieJar()
30             cookieProc=urllib2.HTTPCookieProcessor(cookie)
31         except:
32             raise
33         else:
34             opener=urllib2.build_opener(cookieProc)
35             urllib2.install_opener(opener)       
36 
37     def login(self):
38         print '开始登录'
39         url='http://www.renren.com/PLogin.do'
40         postdata={
41                   'email':self.email,
42                   'password':self.password,
43                   'domain':self.domain  
44                   }
45         req=urllib2.Request(
46                             url,
47                             urllib.urlencode(postdata)            
48                             )
49         
50         self.file=urllib2.urlopen(req).read()
51         idPos=self.file.index("'id':'")
52         self.id=self.file[idPos+6:idPos+15]
53         tokPos=self.file.index("get_check:'")
54         self.tok=self.file[tokPos+11:tokPos+21]
55         rtkPos=self.file.index("get_check_x:'")
56         self.rtk=self.file[rtkPos+13:rtkPos+21]
57     
58 
59 
60     def publish(self,content):
61         url1='http://shell.renren.com/'+self.id+'/status'
62         postdata={
63                   'content':content,
64                   'hostid':self.id,
65                   'requestToken':self.tok,
66                   '_rtk':self.rtk,
67                   'channel':'renren',
68                   }
69         req1=urllib2.Request(
70                             url1,
71                             urllib.urlencode(postdata)            
72                             )
73         self.file1=urllib2.urlopen(req1).read()
74         print '%s:\n刚才使用你的人人账号 %s 发了一条状态\n内容为:(%s)'% (datetime.datetime.now(),self.email,postdata.get('content',''))
75 
76 
77 
78 renrenspider=spider('email','password')
79 renrenspider.login()
80 content=raw_input('请输入状态的内容:')
81 renrenspider.publish(content)

 

posted on 2013-02-05 15:15  紫澜  阅读(655)  评论(0编辑  收藏  举报