躺在沙滩上的小猪

快乐的每一天

自由的python 二

将上面的代码重构了一下,并且添加了cookies,这样我们就不需要每次登陆了

  1#!/usr/bin/python
  2# -*- coding: UTF-8 -*-
  3
  4import urllib
  5import cookielib, urllib2
  6import os
  7import re
  8
  9class Fun:
 10    def __init__(self,username,password,times=1,\                  dataFileName='fun.data',loginurl='',posturl='',\                  proxies={},pform=None,cookies=None):
 11        print '初始化'
 12        self.username=username
 13        self.password=password
 14        self.times = times
 15        self.loginurl=loginurl
 16        self.posturl = posturl
 17        self.proxies=proxies
 18        self.pform=pform        
 19        self.dataFileName=dataFileName
 20        self.cookies=cookielib.CookieJar()
 21        self.opener=None
 22    
 23    def login(self):    
 24        """
 25            登陆
 26        """        
 27        print '登陆'
 28        loginparams = urllib.urlencode({'j_username':self.username,\                                                'j_password':self.password,\
 29                                                                            'UPC_REQUEST_URI':'*.do',\                                                'UPC_LOGIN_FLAG':'UPC_LOGIN_FLAG'}) 
 30        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookies))
 31                        
 32        self.pform = self.opener.open(self.loginurl,loginparams)
 33        print '登陆成功'
 34                        
 35    def post(self):
 36        """
 37            解析登陆完成的HTML页面,获取用户相应的信息
 38        """    
 39        print '解析html页面,获取服务器端返回的内容'
 40        content = '\n'.join(self.pform.readlines())            
 41        items = ["employeeName","department","workArea","workCity","fillInDate"]
 42        values = []
 43        for item in items:
 44            pattern = "name=\""+item+"\"\s*(size=\"\d*\")?\s*\                                    value=\"(\S*(\s*\S*)?)\"\s*id=\""+item+"\""
 45            matcher = re.search(pattern,content)
 46            
 47            if matcher!=None:        
 48                values.append(matcher.groups()[1])
 49                print values
 50            else:
 51                values.append('')
 52        print '解析完成'                
 53        """
 54            提交日报
 55        """        
 56                
 57        print '读取data文件:'+self.dataFileName
 58        f = open(self.dataFileName)
 59        contents = f.readlines()
 60        
 61        #读取data文件,判断是否采用交互界面,还是采用data文件的数据    
 62        
 63        if contents[0]=='n':                    
 64            c = raw_input('是否采用提交当天日志(y/n)?:')
 65            if c=='n':
 66                self.fillInDate = raw_input('请输入提交日期(格式yyyy-mm-dd):')        
 67            projectId = raw_input('项目名称代码:')
 68            workId = raw_input('具体事项:')
 69            workContent = raw_input('工作内容:')
 70            workNum = raw_input('正常工作时长:')
 71            overtimeNum = raw_input('加班工作时长:')
 72        else:
 73            
 74            #是否采用文件中的时间,如果不采用,则采用当前时间
 75            if contents[1]=='y':                
 76                self.fillInDate = contents[2]
 77            
 78            projectId = contents[3]
 79            workId = contents[4]
 80            workContent = contents[5]
 81            workNum = contents[6]
 82            overtimeNum = contents[7]
 83        
 84        #拼凑参数     o_o
 85        paramsmap = {'employeeId':self.username,\                         'employeeName':values[0],\
 86             'department':values[1],\             'workArea':values[2],\             'workCity':values[3],\             'fillInDate':values[4],\
 87             'detailList.projectId':[projectId],\             'detailList.workId':[workId],\
 88             'detailList.workContent':[workContent],\             'detailList.workNum':[0],             'detailList.overtimeNum':[8]}
 89        
 90        keys = paramsmap.keys()
 91        for key in keys:
 92            print key,':',paramsmap[key]
 93        
 94        postparams = urllib.urlencode(paramsmap)
 95        print    'postparams:'+postparams    
 96        #提交
 97        #opener = urllib2.build_opener(self.cookies)       
 98        print '开始提交日报'
 99        result = self.opener.open(self.posturl,postparams)
100        #result = urllib2.urlopen(posturl,postparams)
101        #todo:解析提过完成页面,获取信息
102        #        
103        result.close()
104        print '日报填写成功'
105        
106    def close(self):
107        """
108        """
109        if not self.pform:
110            self.pform.close()
111        
112
113
114if __name__=='__main__':    
115    #测试一下
116    try:
117        test = Fun(username='',password='',loginurl='',posturl = '')
118        test.login()
119        test.post()
120        test.close()
121    except IOError:        
122        raise


初始化
登陆...
登陆成功
解析html页面,获取服务器端返回的内容
['\xd0\xec\xcf\xe9\xbe\xfc']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9', '\xbb\xaa\xb6\xab']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9', '\xbb\xaa\xb6\xab', '\xc4\xcf\x
be\xa9']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9', '\xbb\xaa\xb6\xab', '\xc4\xcf\x
be\xa9', '2005-09-24 17:45:40.0']
解析完成
读取data文件:fun.data
detailList.workId : ['10003102\n']
employeeId : 404
workCity : 南京
detailList.projectId : ['JX0001\n']
detailList.overtimeNum : [8]
detailList.workNum : [0]
detailList.workContent : ['\xbb\xfc\xb2\xe9\xd0\xe8\xc7\xf3\xb7\xd6\xce\xf6\n']
fillInDate : 2005-09-24 17:45:40.0
department : 南京
workArea : 华东
employeeName : 徐祥军
postparams:detailList.workId=%5B%2710003102%5Cn%27%5D&employeeId=404&workCity=%C
4%CF%BE%A9&detailList.projectId=%5B%27JX0001%5Cn%27%5D&detailList.overtimeNum=%5
B8%5D&detailList.workNum=%5B0%5D&detailList.workContent=%5B%27%5Cxbb%5Cxfc%5Cxb2
%5Cxe9%5Cxd0%5Cxe8%5Cxc7%5Cxf3%5Cxb7%5Cxd6%5Cxce%5Cxf6%5Cn%27%5D&fillInDate=2005
-09-24+17%3A45%3A40.0&department=%C4%CF%BE%A9&workArea=%BB%AA%B6%AB&employeeName
=%D0%EC%CF%E9%BE%FC
开始提交日报
日报填写成功

posted on 2005-09-24 18:22 martin xus 阅读(568) 评论(5)  编辑  收藏 所属分类: python