python模拟登录人人网实例代码

发布时间:2020-10-09编辑:脚本学堂
本文介绍了python实现登录人人网的一例代码,python使用urllib2、cookielib模块实现模拟登录的例子,需要的朋友参考下。

例子,python/login/ target=_blank class=infotextkey>python模拟登录人人网
 

复制代码 代码示例:

import re
import urllib2
import cookielib
def renren():
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    email = ''
    pwd   = ''

    #登录..
    print 'login......'
    url = "http://www.renren.com/PLogin.do"
    postdata = "email="+email+"&password="+pwd+"&origURL=http%3A%2F%2Fwww.renren.com%2FSysHome.do&domain=renren.com"
    req = urllib2.Request(url,postdata)
    res = opener.open(req).read()
    print 'succeed!'

    #得到当前状态
    s = r'(?s)id="currentStatus">.*?<a ui-async="async" title="([^"]*)'
    match = re.search(s, res, re.DOTALL)
    if match:
        result = match.groups(1)
        print 'current status: ', result[0]
renren()