zoukankan      html  css  js  c++  java
  • python 模拟登陆校内 cookielib urllib2

      进来学习了一下python的,感觉还是很不错的,到处找东西去练习,在老王python那里也能够有一些基础的练习。很适合我。           

    模拟登陆
     1 #encoding=gbk
     2 
     3 import sys
     4 import re
     5 import cookielib
     6 import urllib2
     7 import urllib
     8 
     9  
    10 
    11 class Renren(object):
    12 
    13     def __init__(self):
    14         self.name=self.pwd=self.domain=self.origURL=self.operate=""
    15         self.cj=cookielib.LWPCookieJar()
    16         try:
    17             self.cj.revert('renren.cookie')
    18         except Exception,e:
    19             print e
    20         self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
    21         urllib2.install_opener(self.opener)
    22 
    23     def setinfo(self,username,pwd,domain,origURL):
    24         """设置登陆参数"""
    25         self.name=username
    26         self.pwd=pwd
    27         self.domain=domain
    28         self.origURL=origURL
    29 
    30     def login(self):
    31         """登陆人人网"""
    32         params={'domain':self.domain,'origURL':self.origURL,'email':self.name,
    33                 'password':self.pwd}
    34         print 'login......'
    35         req=urllib2.Request(
    36             'http://www.renren.com/PLogin.do',
    37             urllib.urlencode(params))
    38         self.operate = self.opener.open(req)
    39         if self.operate.geturl() == 'http://www.renren.com/Home.do':
    40             print 'logged on successfully!'
    41             self.cj.save("renren.cookie")
    42             self.__viewinfo()
    43         else:
    44             print 'login error.....'
    45 
    46     def __viewinfo(self):
    47         """查看个人的状态"""
    48         print "正在获取信息......"
    49         self.__caiinfo()
    50         
    51     def __caiinfo(self):
    52         h3patten=re.compile('^<meta.*/>$')#正则匹配,这里假设匹配元信息
    53         incontent=self.operate.readlines()#读取整个网页内容
    54         #print incontent
    55         for i in incontent:
    56             content=h3patten.findall(i)
    57             if len(content)!=0:
    58                 for ok in content:           
    59                     print ok.decode("utf-8").encode("gbk")   #进行编码的转换,否则显示是十六进制的数     
    60         print "已完成!"         
    61 if __name__=='__main__':
    62             ren=Renren()
    63             ren.setinfo("xxxx@gmail.com",'xxxx',
    64                         "renren.com","http://www.renren.com/SysHome.do")
    65             ren.login()
    66             
    67 

    程序自动在当前目录下生成renren.cookie,我这里显示是:

    #LWP-Cookies-2.0
    Set-Cookie3: depovince=GX; path="/"; domain=".renren.com"; path_spec; domain_dot; expires="2010-08-07 04:01:17Z"; version=0
         在这里比较麻烦的是在59行处,花了我好久的时间,也算是python的老问题了,也就是编码的问题,python默认使用utf-8进行编码的处理,当我们要处理中文的时候,往往会变成了乱码,decode是解码,encode是编码,经过两步的处理,才能够很好的显示中文。在这里做一个记录。

    作者:xiaoxia

    出处:http://cnblogs.com/xiaoxia

    我的淘宝:http://shop62115161.taobao.com/

    本文遵从GNU 的自由文档许可证(Free Document License)的条款,欢迎转载、修改、散布。 

  • 相关阅读:
    利用web.confing保存数据
    sql2005 未知错误 MS Visual Database Tools
    安装SQL2005时系统配置检查出现“性能监视器计数器要求”
    DNN中的Unit Test
    介绍一个收费的SVN Host
    DNN Navisuite菜单模块原始XML文件的格式
    如何使用C#调用Java
    介绍几个DNN SEO模块,可免费试用的
    帮你找到色彩Photoshop LAB Color
    Google PR 到4了
  • 原文地址:https://www.cnblogs.com/xiaoxia/p/1792461.html
Copyright © 2011-2022 走看看