Python小练习-自动登录人人发送消息并返回好友列表
主要是由urllib,urllib2,cookielib这三个模块。对这三个标准库不熟悉可以学习一下,我也不是很熟系,练习一下。
比如 urllib2 - The Missing Manual
主要就是模拟人人登录,通过httpfox抓包来对登录网站的过程进行分析,将密码和发送的消息post到服务器上就可以,用cookielib模块处理cookie,然后就是正则匹配的问题了。
1 #!/usr/bin/env python 2 #coding=utf-8 3 import urllib 4 import urllib2 5 import cookielib 6 import re 7 from sgmllib import SGMLParser 8 from datetime import * 9 class Loginrenren(SGMLParser): 10 friendlist = [] 11 header = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0'} 12 def __init__(self,username,password): 13 SGMLParser.__init__(self) 14 self.username = username 15 self.password = password 16 cj = cookielib.CookieJar() 17 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 18 urllib2.install_opener(opener) 19 def login(self): 20 '''login in and return uid''' 21 logpage = "http://www.renren.com/Login.do" 22 data = {'email':self.username,'password':self.password} 23 login_data = urllib.urlencode(data) 24 res = urllib2.Request(logpage,login_data) 25 self.file=urllib2.urlopen(res).read() 26 idPos = self.file.index("'id':'") 27 self.uid=self.file[idPos+6:idPos+15] 28 tokPos=self.file.index("get_check:'") 29 self.tok=self.file[tokPos+11:tokPos+21] 30 rtkPos=self.file.index("get_check_x:'") 31 self.rtk=self.file[rtkPos+13:rtkPos+21] 32 #html = res.read() 33 print 'login now...' 34 print "Getting user id of you now" 35 res = urllib2.urlopen("http://www.renren.com/home") 36 html = res.read() 37 uid = re.search("'ruid':'(\d+)'",html).group(1) 38 print "login and got uid successfully" 39 return uid 40 def getfriendlist(self): 41 req = urllib2.Request(url='http://friend.renren.com/myfriendlistx.do',headers = self.header) 42 result = urllib2.urlopen(req).read() 43 friend = str(re.search('friends=\[{.*}\]',result).group()) 44 friendId = re.findall(r'"id":(.*?),.*?,"name":"(.*?)"',friend) 45 for f in friendId: 46 self.friendlist.append(f) 47 print f[1].decode('unicode-escape') 48 def postmessage(self,content): 49 url = "http://shell.renren.com/"+self.uid+"/status" 50 postdata = {'content':content, 51 'hostid':self.uid, 52 'requetToken':self.tok, 53 '_rtk':self.rtk, 54 'channel':'renren', 55 } 56 log_data = urllib.urlencode(postdata) 57 req2 = urllib2.Request(url,log_data) 58 self.hax = urllib2.urlopen(req2).read() 59 print '在'+datetime.now().strftime('%Y-%m-%d %H:%M:%S %p')+' 你发送了一条为 "'+content+'" 的消息' 60 users = Loginrenren('youremailaddress',"yourpassword") 61 users.login() 62 users.getfriendlist() 63 content = raw_input("请输入内容:") 64 users.postmessage(content)
requestToken和_rtk不知道怎么求,查的。
其他不说了,代码不难,忘了谁说的了,代码可以自己解释自己 - -