#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:本代码有大量参考,具体引用网址找不到了,只能向原作者致敬了….