将上面的代码重构了一下,并且添加了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 = ' '.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
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2015-05-04 00:00:00
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/1870827/
本文WWW.DOC100.NET DOC100.NET版权所有。