返回密码[Python]小练习 模拟登陆人人网

本篇文章朋友在北京喝咖啡的时候突然想到的...最近就有想写几篇关于返回密码的笔记,所以回家到之后就奋笔疾书的写出来发布了

    总得思绪很简单:

    获得一个cookie

    装载好自己的request

    发送请求

    

    每日一道理
站在历史的海岸漫溯那一道道历史沟渠:楚大夫沉吟泽畔,九死不悔;魏武帝扬鞭东指,壮心不已;陶渊明悠然南山,饮酒采菊……他们选择了永恒,纵然谄媚诬蔑视听,也不随其流扬其波,这是执著的选择;纵然马革裹尸,魂归狼烟,也要仰天长笑,这是豪壮的选择;纵然一身清苦,终日难饱,也愿怡然自乐,躬耕陇亩,这是高雅的选择。在一番选择中,帝王将相成其盖世伟业,贤士迁客成其千古文章。
#coding:utf8 
'''
模拟陆登人人 根据网上的资料和firefox做了下 
首先自己去探查了下
面页素元:
<input id="email" class="input-text" type="text" value="" tabindex="1" name="email" style="color: rgb(136, 136, 136);"></input
<input id="password" class="input-text" type="password" autocomplete="off" tabindex="2" error="请输入密码" name="password"></input>

cookie:
 jebecookies=523a9b12-658f-43c0-abf8-1ca1f3f87c10|||||; domain=.renren.com; path=/

'''
import re,urllib,urllib2,cookielib,codecs,chardet,sys 

class LoginRenRen():
    
    def __init__(self,name='',password='',domain=''):
        self.name=name
        self.password=password
        self.domain=domain
        
        self.cj = cookielib.LWPCookieJar()
        try:
            #主动去程远获得一个cookie
            #self.cj.revert('renren.coockie')  #试测的时候每次都有异常但是毫无影响,注释掉了也不影响
            print 'successed got a cookie..'
        except Exception,e:
            print 'Can not get the cookies',e.message
        
        #装载cookies  
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)
        
    def login(self):
        params = {'domain':self.domain,'email':self.name,'password':self.password}
        req = urllib2.Request(
            'http://www.renren.com/PLogin.do',
            urllib.urlencode(params)
        )
        print 'login.....'
        
        self.openrate = self.opener.open(req)
        
        #查看下返回的url来判断陆登进去了没
        print self.openrate.geturl()
        #info = self.openrate.read()
        
        #查看了一下面页码编 chardet是第三方库
        #print chardet.detect(info)
        print ''        
        #print re.findall(r'password',info)
        #打印出了返回的容内
        type = sys.getfilesystemencoding()   
        #print info.decode("UTF-8").encode(type) 
        
            
if __name__=='__main__':
    username = 'liuzhizhi123@126.com' #用户名
    password = '4933848liu' #密码
    domain = 'renren.com'
    ren = LoginRenRen(username,password,domain)
    ren.login()

    

文章结束给大家分享下程序员的一些笑话语录: 刹车失灵
有一个物理学家,工程师和一个程序员驾驶着一辆汽车行驶在阿尔卑斯山脉 上,在下山的时候,忽然,汽车的刹车失灵了,汽车无法控制地向下冲去, 眼看前面就是一个悬崖峭壁,但是很幸运的是在这个悬崖的前面有一些小树 让他们的汽车停了下来, 而没有掉下山去。 三个惊魂未定地从车里爬了出来。
物理学家说, “我觉得我们应该建立一个模型来模拟在下山过程中刹车片在高 温情况下失灵的情形”。
工程师说, “我在车的后备厢来有个扳手, 要不我们把车拆开看看到底是什么 原因”。
程序员说,“为什么我们不找个相同的车再来一次以重现这个问题呢?”

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3033775.html