python之renren网更新状态机器人

#encoding=utf-8
import urllib2
import urllib
import cookielib
import re
  
def found_id_cookie(cookieJar):
  for item in cookieJar:
    #print item.name;
    if item.name=='id':
      return item.value;
    #else:
      #return 'failed to get the value of id';
  return 'false';
  
def renrenBrower(url,user,password):
    #登陆页面,可以通过抓包工具分析获得,如fiddler,wireshark
    login_page = "http://www.renren.com/PLogin.do"
    try:
        #获得一个cookieJar实例
        cj = cookielib.CookieJar()
        #cookieJar作为参数,获得一个opener的实例
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
        urllib2.install_opener(opener);
        #伪装成一个正常的浏览器,避免有些web服务器拒绝访问。
        opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
        #生成Post数据,含有登陆用户名密码。
        data = urllib.urlencode({"email":user,"password":password})
        #以post的方法访问登陆页面,访问之后cookieJar会自定保存cookie
        req=urllib2.Request(login_page,data);
        resp=urllib2.urlopen(req).read();
        #获取用户id
        userid=found_id_cookie(cj);
        #以带cookie的方式访问页面
        if userid=='false':
        	  print 'failed to login renren';
	    else:
	         print 'success to login renren,update status ing.........'
	         print 'to find _rtk';
	        #XN = {get_check:'-1902747724',get_check_x:'5a575e0e',env:{domain:'renren.com',shortSiteName:'人人',siteName:'人人网'}};photoSeedSrc= 'http://s.xnimg.cn/a59624/n/apps/photo/photoSeed.js';
	        string_path='get_check_x:\'(?P<_rtk>.*?)\'';
            rtk=re.search(string_path,resp).group('_rtk');
            print rtk
            update_status(userid,rtk);
            #return data
    except Exception,e:
        print str(e)
        
def update_status(userid,rtk):
  post_path='http://shell.renren.com/'+userid+'/status';
  content='this is for test kaixin223333333333333333!';
  post_data=urllib.urlencode({
    'content':content,
    'privacyParams':"{'sourceControl': 99}",
    #'requestToken':'-1702978444',
    '_rtk':rtk,
    'channel':'renren',
    });
  req=urllib2.Request(post_path,post_data);
  resp=urllib2.urlopen(req);
  
#访问某用户的个人主页,其实这已经实现了人人网的签到功能。
user='youremail'
password='yourpassword'
#print renrenBrower("http://www.renren.com/home",user,password)
renrenBrower("http://www.renren.com/home",user,password)
ps:本代码有大量参考,具体引用网址找不到了,只能向原作者致敬了….
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值