1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
import requests from http import cookiejar from bs4 import BeautifulSoup from PIL import Image import pytesseract import time import datetime import codecs import os
headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8','Accept-Encoding':'gzip, deflate','Accept-Language':'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2','Cache-Control':'max-age=0','Connection':'keep-alive','Content-Length':'559','Content-Type':'application/x-www-form-urlencoded','Host':'eds.topcheer.com','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'} h1 = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8','Accept-Encoding':'gzip, deflate','Accept-Language':'en-US,en;zh-CN','Connection':'keep-alive','DNT':'1','Host':'eds.topcheer.com','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
class DynamicKey: VIEWSTATE = ''; VIEWSTATEGENERATOR = ''; EVENTVALIDATION = ''; def __init__(self, vs, vsg, ev): self.VIEWSTATE = vs; self.VIEWSTATEGENERATOR = vsg; self.EVENTVALIDATION = ev; def __str__(self): return "{VIEWSTATE:%s,VIEWSTATEGENERATOR:%s,EVENTVALIDATION:%s}" % (self.VIEWSTATE, self.VIEWSTATEGENERATOR, self.EVENTVALIDATION)
def get_cookies(): cj = cookiejar.LWPCookieJar(filename='cookies_eds') return cj try: cj.load() except: print "load cookie failed." exit() def get_dynamickey(html):
soup = BeautifulSoup(html, 'html.parser') vs = soup.find(attrs={'name':'__VIEWSTATE'}) vsg = soup.find(attrs={'name':'__VIEWSTATEGENERATOR'}) ev = soup.find(attrs={'name':'__EVENTVALIDATION'}) dk = DynamicKey(vs['value'], vsg['value'], ev['value'])
return dk
def login(): url = "http://eds.topcheer.com"
r = s.get(url, headers=h1) s.cookies.save(ignore_discard=True) r.encoding='utf-8' dk = get_dynamickey(r.text) data = { '__EVENTTARGET':'', '__EVENTARGUMENT':'', '__VIEWSTATE':dk.VIEWSTATE, '__VIEWSTATEGENERATOR':dk.VIEWSTATEGENERATOR, '__EVENTVALIDATION':dk.EVENTVALIDATION, 'tbUserId':'', 'tbPassword':'', 'ctl04':'' } r = s.post('http://eds.topcheer.com/index.aspx', headers=h1, data=data) s.cookies.save(ignore_discard=True) print '登录成功' return;
def query_overtime_list(): t = datetime.datetime.now() starttmp = str(t.year)+str(t.month-1)+'01' start = time.strftime("%Y/%m/%d", time.strptime(starttmp, '%Y%m%d')) end = time.strftime("%Y/%m/%d", time.localtime()) print start print end r = s.post('http://eds.topcheer.com/ajax/EDS.Web.AjaxMethod.Common,EDS.ashx?_method=SetCurrentModelID&_session=rw', headers=h1, data={'modelID':'44'}) r = s.get('http://eds.topcheer.com/UI/AttendanceMng/OvertimeList.aspx', headers=h1) dk = get_dynamickey(r.text) data = { '__EVENTTARGET':'', '__EVENTARGUMENT':'', '__VIEWSTATE':dk.VIEWSTATE, '__VIEWSTATEGENERATOR':dk.VIEWSTATEGENERATOR, '__EVENTVALIDATION':dk.EVENTVALIDATION, 'datOvertimeStartDate$txt_Date':start, 'datOvertimeStartDate$controlName':'', 'datOvertimeEndDate$txt_Date':end, 'datOvertimeEndDate$controlName':'', 'ddlApplyType':'0', 'ddlOrgName$menuLevelID':'4', 'ddlOrgName$orgID':'17', 'ddlOrgName$OrgIDString':'( 17)', 'txtEmployeeNM':'', 'ddlApplyStatus':'0', 'btnSearch':'', 'gridPage$tbPage':'' } r = s.post('http://eds.topcheer.com/UI/AttendanceMng/OvertimeList.aspx', headers=h1, data=data) s.cookies.save(ignore_discard=True) soup = BeautifulSoup(r.text, 'html.parser') total = soup.find(attrs={'id':'ulblGridCount'}) print total tb = soup.find(attrs={'class':'Grid'}) print tb
s = requests.Session() s.cookies = get_cookies() login() query_overtime_list()
|