def clean_desk(u): is_tmv = join(IMG_PATH, 'clean_desk', 'tv_enter.png') click_enter = join(IMG_PATH, 'clean_desk', 'zan.png') if u.btn_location(is_tmv, loop_time=3): res = u.btn_location(click_enter) if res: u.click_point(res[0] + 102, res[1]) return True
def handle_login_res(loginid): result = btn_location(img_path) # vc page if result: logger.info('Verify Code Appeared') return deal_vc(loginid) elif btn_location(err_account_img): # account error page kill_qq() logger.info('Wrong account or password!') res = False elif btn_location(death_acc_img): kill_qq() logger.info('Frozen account') res = False elif btn_location(find_password_img): kill_qq() logger.info('Wrong password! Find and recheck') res = False elif btn_location(authentication_img): kill_qq() logger.info('Need to authentication!') res = False elif btn_location(login_success): logger.info('Tim client login success') return True else: logger.info('Unknown situation with account: %s' % ACC) res = False if not res: pic_name = join(NEW_ERROR_PATH, 'error_%s.png' % (int(time.time()))) pag.screenshot(pic_name) return res
def deal_vc(self): # 裁剪 element = self.d.find_element_by_id('img-captcha') img_path = join(IMG_PATH, 'vc.png') u.cutimg_by_driver(self.d, element, img_path) with open(img_path, 'br') as i: img = i.read() vc_res = u.rc.rk_create(img, 3040) vc = vc_res.get('Result').lower() # 验证 self.d.find_element_by_name('entered_imagecode').send_keys(vc) self.d.find_element_by_id('btn-login').click() time.sleep(2) check_cookie = self.d.get_cookies() res = self.is_login(check_cookie) if not res.get('succ'): login_res = self.d.find_element_by_xpath('//div[@class="mod-login-inner"]//span').text if login_res == '验证码错误': u.rc.rk_report_error(vc_res.get('Id')) return {'succ': False, 'msg': 'vc'} elif login_res == '用户名密码错误': return {'succ': False, 'msg': 'pd'} u.rc.rk_report(img, 3040, vc, vc_type=self.user_info.get('platform')) return {'succ': True}
def deal_vc(self): # 裁剪 element = self.d.find_element_by_id('token-img') img_path = join(IMG_PATH, 'vc.png') u.cutimg_by_driver(self.d, element, img_path) with open(img_path, 'br') as i: img = i.read() vc = u.rc.rk_create(img, 3040)['Result'].lower() # 验证 self.d.find_element_by_id('uc-common-token').send_keys(vc) self.d.find_element_by_id('submit-form').click() time.sleep(2) # res = self.is_login(check_cookie) try: self.d.find_element_by_class_name('logout') return {'succ': True, 'msg': 'login success'} except: logger.info('no logout btn, mean not login, pass to vc_pwd step') vc_res = self.d.find_element_by_id('token-error').text pwd_res = self.d.find_element_by_id('account-error').text if vc_res == '验证码错误': return {'succ': False, 'msg': 'vc'} if pwd_res == '用户名密码错误': return {'succ': False, 'msg': 'pd'}
def get_vc_img(self): try: url = self.d.find_element_by_class_name( 'geetest_item_img').get_attribute('src') except: return { 'succ': False, 'msg': 'slider verify, refresh to click verify' } logger.info('img_url: %s' % url) headers = { 'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", 'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36", } res = get(url, headers=headers) if not res.get('is_success'): return {'succ': False, 'msg': 'net error'} img = res.get('msg').content with open(join(IMG_PATH, 'click_vc.png'), 'bw') as i: i.write(img) vc = u.rc.rk_create(img, 6906, timeout=90) del (img) # if not vc.get('Result'): # u.rc.rk_report_error(vc.get('Id')) # return self.get_vc_img() return {'succ': True, 'msg': vc}
def get_img(self, sd, ed, flow=False): # 报表部分 url = f'https://ad.oceanengine.com/overture/reporter/advertiser/?st={sd}&et={ed}' js = None if flow: # 财务流水部分 url = 'https://ad.oceanengine.com/overture/cash/flow/' with open(join(JS_PATH, 'ad_toutiao.js'), 'r', encoding='utf-8') as j: js = j.read() self.d.get(url) self.d.implicitly_wait(10) delete_mask = """ ele = document.querySelector('.user-guide'); if (ele){ele.remove();}""" self.d.execute_script(delete_mask) if flow: self.d.execute_script(js % (sd, ed)) pic_name = f'{sd}_{ed}.png' if not flow else f'财务流水_{sd}_{ed}.png' height = self.d.execute_script('return a=document.body.offsetHeight') cut_res = cut_img(height, self.dir_path, pic_name) if not cut_res.get('succ'): logger.error('got pic failed --- pic_name: %s' % pic_name) logger.info('got an pic: %s' % pic_name) return {'succ': True}
def init_paths(self, ui): """初始化路径数据""" init_dst_dir(self.platform, isCpa=self.is_cpa) # 创建文件夹 cur_time = time.strftime('%Y-%m-%d') dir_name = '%(taskId)s_%(cTime)s_%(account)s' % {'taskId': ui['id'], 'cTime': time.strftime('%Y-%m-%d_%H-%M-%S'), 'account': ui.get('account')} self.dir_path = settings.join(settings.sd_path, self.platform, cur_time, dir_name) os.makedirs(self.dir_path) self.err_img_name = settings.join(self.dir_path, 'error_%s_%s.jpg' % (int(time.time()*1000), task_type)) self.dst_path = '/data/python/%s/%s/%s' % (self.platform, cur_time, dir_name) if self.is_cpa: self.dst_path = '/data/python/%s/%s/%s/%s' % ('CPA', self.platform, cur_time, dir_name) settings.DST_DIR = self.dst_path # Add to login class for post res
def preload_window(self): loadtime = """return {"requestTime":1531198164.528,"startLoadTime":1531198164.528,"commitLoadTime": 1531198165.871,"finishDocumentLoadTime":1531198168.69,"finishLoadTime":1531198171.872,"firstPaintTime": 1531198166.734,"firstPaintAfterLoadTime":0,"navigationType":"Other","wasFetchedViaSpdy":false, "wasNpnNegotiated":true,"npnNegotiatedProtocol":"http/1.1","wasAlternateProtocolAvailable":false, "connectionInfo":"http/1.1"}""" repstr = """return {"requestTime":%(requestTime)s,"startLoadTime":%(startLoadTime)s, "commitLoadTime":%(commitLoadTime)s,"finishDocumentLoadTime":%(finishDocumentLoadTime)s, "finishLoadTime":%(finishLoadTime)s,"firstPaintTime":%(firstPaintTime)s,"firstPaintAfterLoadTime":0, "navigationType":"Other","wasFetchedViaSpdy":false,"wasNpnNegotiated":true,"npnNegotiatedProtocol": "http/1.1","wasAlternateProtocolAvailable":false,"connectionInfo":"http/1.1"}""" cutime = time.time() timedict = { 'requestTime': int(cutime * 1000) / 1000, 'startLoadTime': int(cutime * 1000 + 51) / 1000, 'commitLoadTime': int(cutime * 1000 + 102) / 1000, 'finishDocumentLoadTime': int(cutime * 1000 + 104) / 1000, 'finishLoadTime': int(cutime * 1000 + 105) / 1000, 'firstPaintTime': int(cutime * 1000 + 104) / 1000 } with open(join(JS_PATH, 'preload.js')) as j: data = j.read() for e in data.split('\n'): if e.strip() == loadtime: e.replace(loadtime, repstr % timedict) self.d.execute_script(data)
def get_img(self, data, sd, ed): # 跳转数据报表页面 url = 'https://e.qq.com/atlas/%(uid)s/report/order?ptype=%(tid)s&pid=%(pid)s&pname=%(pn)s' % { 'uid': self.uid, 'tid': data.get('type_id'), 'pid': data.get('pdata').get('pid'), 'pn': data.get('pdata').get('pname') } self.d.get(url) pic_name = '%(ptype)s_%(productId)s_%(productName)s_%(sd)s_%(ed)s.png' % { 'ptype': data.get('tname'), 'productId': data.get('pdata').get('pid'), 'productName': data.get('pdata').get('pname'), 'sd': sd, 'ed': ed } # 图片命名 time.sleep(3) try: self.wait_element(By.LINK_TEXT, '查看报表', wait_time=6).click() except: self.d.execute_script('document.querySelector(".button-more").click()') # self.wait_element(By.CLASS_NAME, 'button-more').click() time.sleep(1) # 调整分页数量 with open(join(JS_PATH, 'e_qq_pagenum.js'), 'r') as p: pjs = p.read() self.d.execute_script(pjs) time.sleep(1.5) self.d.switch_to.frame(self.d.find_element_by_css_selector('.splitview-tabs-body iframe')) # 获取高度 get_height = 'return a=document.querySelector("#content").offsetHeight' height = self.d.execute_script(get_height) cut_res = cut_img(height, self.dir_path, pic_name) if not cut_res['succ']: logger.error( 'get img %s_%s failed-------msg: %s' % (data.get('tname'), data.get('pdata')[1], cut_res['msg'])) logger.info('height: %s ---picname: %s' % (height, pic_name)) time.sleep(1) logger.info('got an img: picname--%s' % pic_name)
def get_data(self, sd, ed, data, tid, tname): logger.info('get into (self.get_data_common_version)function') url = "https://e.qq.com/ec/api.php" fname = '%(productType)s_%(productId)s_%(productName)s_%(sd)s_%(ed)s.json' % { 'productType': tname, 'productId': data.get('pid'), 'productName': data.get('pname'), 'sd': sd, 'ed': ed } # make params pdata = { "sdate": sd, "edate": ed, "product_type": tid, 'g_tk': self.g_tk, "product_id": data.get("pid"), "owner": self.uid } headers = { "cookie": self.cookie_str, "referer": "http://e.qq.com/atlas/%(uid)s/report/order_old?pid=%(pid)s&ptype=%(ptype)s" % { 'uid': self.uid, 'pid': data.get('pid'), 'ptype': tid } } self.params['gtk'] = self.g_tk self.pdata.update(pdata) self._headers.update(headers) data = post(url, data=self.pdata, params=self.params, headers=self._headers) if not data['is_success']: return {'succ': False, 'msg': data.get('msg')} file_name = join(self.dir_path, fname) data = data['msg'].json() cost = data.get('data').get('total').get('cost').replace(',', '') if float(cost) == 0: return {'succ': False, 'msg': 'no data'} with open(file_name, 'w', encoding='utf-8') as f: json.dump(data, f) logger.info('crawled data: %s' % data) return {'succ': True}
def login(self, ui): self.d.get('https://unicorn.wcd.qq.com/login.html') time.sleep(1) inpUsername = self.wait_element(By.ID, 'username') inpUsername.clear() inpUsername.send_keys(ui['account']) inpPassword = self.d.find_element_by_id('password') inpPassword.clear() inpPassword.send_keys(ui['password']) time.sleep(1) vcimgpath = settings.join(settings.IMG_PATH, 'app_imgs', 'wcdVerifyCode.png') vcodeimg = self.d.find_element_by_id('v-code-img') u.cutimg_by_driver(self.d, vcodeimg, vcimgpath) # im = None # with open(vcimgpath, 'rb') as f: # im = f.read() # lk = u.rc.rk_create(im, '3040').get('Result') lk = self.realizeCode(vcimgpath) inpVcode = self.d.find_element_by_id('v-code') inpVcode.send_keys(lk) btnLogin = self.d.find_element_by_css_selector('.btn-login') btnLogin.click() time.sleep(4) loginRes = self.isLogin() logger.info('isLogin:%s' % loginRes) if self.isLogin(): return {'succ': True, 'cookies': self.d.get_cookies()} else: return {'succ': False, 'msg': 'login fail'}
def deal_vc(self): # 裁剪 element = self.d.find_element_by_id('captcha-img') img_path = join(IMG_PATH, 'vc.png') u.cutimg_by_driver(self.d, element, img_path) with open(img_path, 'br') as i: img = i.read() vc_res = u.rc.rk_create(img, 3050) vc = vc_res.get('Result').lower() # 验证 self.d.find_element_by_id('captcha-code').send_keys(vc) self.d.find_element_by_id('login-button').click() res_xpath = "//form[@id='login-main-form']//span[@class='error-con']" res_text = self.d.find_element_by_xpath(res_xpath).text if res_text == '验证码不正确': time.sleep(1) u.rc.rk_report_error(vc_res.get('Id')) self.deal_vc() elif res_text == '用户名或密码不正确': return {'succ': False, 'msg': 'login failed'} else: u.rc.rk_report(img, 3050, vc, vc_type=self.user_info.get('platform')) return {'succ': True}
def set_date(self, sd, ed): """更新起止日期""" with open(join(JS_PATH, 'baidu_message_stream.js'), 'r', encoding='utf-8') as f: # 相对于当前文件路径写法 setDateJs = f.read() setDateJs = setDateJs % {'datest': sd, 'dateet': ed} self.d.execute_script(setDateJs)
def getLogger(self, platform): global logger log_path = settings.join(settings.LOGPATH, platform) if not os.path.exists(log_path): os.makedirs(log_path) settings.GlobalVal.CUR_MAIN_LOG_NAME = platform logger = u.record_log(log_path, platform) self.logger = logger
def update_date_use_js(self, sd, ed): with open(join(JS_PATH, 'mi_vue_set_date.js'), 'r', encoding='utf-8') as f: date_js = f.read() sd = '/'.join(sd.split('-')) ed = '/'.join(ed.split('-')) js_text = '%s("%s", "%s");' % (date_js, sd, ed) self.d.execute_script(js_text) self.d.implicitly_wait(10) time.sleep(1)
def deal_vc(self): vc_name = join(IMG_PATH, 'vc_img.png') self.d.save_screenshot(vc_name) element = self.d.find_element_by_css_selector('.Captcha__img') u.cutimg_by_driver(self.d, element, vc_name) with open(vc_name, 'br') as f: im = f.read() res = u.rc.rk_create(im, '1060') vk = res.get('Result') return res, vk, im
def close_chrome_debugger(self, delete_user_data=False): try: self.d.quit() # self.debug_chrome.kill() os.system(f'taskkill /PID {self.debug_chrome.pid}') except: pass if delete_user_data: data_dir = join(self.base_chrome_data_dir, self.base_profile_name) os.system(f'rm -rf {data_dir}')
def getRecallReportData(self, cookies, osd, oed): logger.info('getRecallReportData|start osd:%s oed:%s' % (osd, oed)) ckstr = '; '.join( ['%s=%s' % (e.get('name'), e.get('value')) for e in cookies]) headers = { 'Cookie': ckstr, 'Referer': 'http://qimeng.iqiyi.com/qimengUI/wake/out', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' } platforms = ['iPhone', 'GPhone'] # 写在js中不常更改 # 渠道码请求(可以一次多选) url = "http://qimeng.iqiyi.com/qimengnew/recallReport/cbCodeConfigList" res = requests.get(url, headers=headers) if res.status_code != 200: logger.error('getRecallReportData|status code not 200: %s' % res.status_code) return False res = res.content.decode('utf-8') res = json.loads(res) cbCodes = res.get('data') # 拉起list请求 for platform in platforms: for cbCode in cbCodes: url = 'http://qimeng.iqiyi.com/qimengnew/recallReport/list?groupName=&startDate=%s&endDate=%s&platForm=%s&codeIds=%s&showFiledId=4&limit=200&offset=1' % ( osd, oed, platform, cbCode.get('colSubChlId')) res = requests.get(url, headers=headers) if res.status_code != 200: logger.error( 'getRecallReportData|status code not 200: %s' % res.status_code) return False res = res.content.decode('utf-8') res = json.loads(res) # 写入文件 filepath = join( self.dir_path, '%s_%s_recallReport_%s_%s.json' % (osd, oed, platform, cbCode.get('colSubChlName'))) with open(filepath, 'w', encoding='utf-8') as f: f.write(json.dumps(res, ensure_ascii=False)) logger.info('getRecallReportData|写入文件成功:%s' % filepath) return { 'succ': True, 'data': { 'platform': platforms, 'cCodes': cbCodes } }
def get_data(self, cookie, osd, oed): channels = self.get_channel(cookie) for channel in channels: alldata = self.get_data_by_channel(channel, cookie, osd, oed) filepath = join(self.dir_path, '%s_%s_%s.json' % (osd, oed, channel)) with open(filepath, 'w', encoding='utf-8') as f: f.write(json.dumps(alldata, ensure_ascii=False)) logger.info('get_data|文件写入成功%s' % filepath) time.sleep(0.5) return {'succ': True, 'data': channels}
def deal_vc(self): vc_name = join(IMG_PATH, 'vc_img.png') self.d.save_screenshot(vc_name) element = self.d.find_element_by_css_selector( 'section div:nth-child(3) div:nth-child(6) div') u.cutimg_by_driver(self.d, element, vc_name, chx=-15, chy=0) with open(vc_name, 'br') as f: im = f.read() res = u.rc.rk_create(im, '3040') vk = res.get('Result') return res, vk, im
def deal_vc(self): ele = self.wait.until(EC.visibility_of_element_located((By.ID, 'checkpic'))) img_path = join(IMG_PATH, 'vc.png') Util().cutimg_by_driver(self.d, ele, img_path) # with open(img_path, 'br') as i: # im = i.read() vc = self.ch_img(img_path) if not vc: ele.click() return self.deal_vc() return vc
def click_function(self): imgs_path = join(IMG_PATH, 'aliyun_imgs') img_dicts = { 'user_name': join(imgs_path, 'user_name.png'), 'passwd': join(imgs_path, 'passwd.png'), 'btn_login': join(imgs_path, 'btn_login.png'), 'vc_block': join(imgs_path, 'verify_box.png') } un_lc = u.click_img_center(img_dicts['user_name']) if not un_lc: logger.warning('do not get user_name location') return {'succ': False, 'msg': 'do not get user_name location'} time.sleep(1) u.pag.typewrite(self.acc, interval=0.3) # 输入用户名 # self.key_input(st=self.acc, press_sleep=0.3) time.sleep(2) u.pag.click(140, 115) pd_lc = u.click_img_center(img_dicts['passwd']) if not pd_lc: return {'succ': False, 'msg': 'do not get password location'} # self.key_input(st=self.pwd, press_sleep=0.3) u.pag.typewrite(self.pwd, interval=0.3) # 输入密码 time.sleep(2) if not u.btn_location(img_dicts['btn_login']): # 寻找登陆btn return {'succ': False, 'msg': 'do not get btn_login location'} # 滑块验证 path length: 248 for x in range(4): u.click_img_center(img_dicts['btn_login']) # 点击登录 time.sleep(2) vc_location = u.btn_location(img_dicts.get('vc_block'), loop_time=3) if vc_location: # 寻找验证滑块 self.slide_vc_block(vc_location) if not u.btn_location(img_dicts['btn_login']): # 寻找登陆btn break else: # 三次不通过就是失败 return {'succ': False, 'msg': 'do not get btn_login location'} return {'succ': True}
def get_main_content(self, p_cf, cookie, sd, ed): # 获取主账号的数据,并且完成截图 # get_data------ids url = 'https://e.yunos.com/api/campaign/list/layout' headers = { 'accept': "application/json, text/javascript", 'content-type': "application/x-www-form-urlencoded", 'cookie': cookie, 'referer': "https://e.yunos.com/", 'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36", } data = get(url, headers=headers) if not data.get('is_success'): return data id_list = json.loads(data.get('msg').content.decode('utf-8')) if id_list.get('total_count') == 0: return {'succ': False, 'msg': 'no data', 'account': self.acc} ids = [(e.get('id'), e.get('title')) for e in id_list.get('data') if '东方' in e.get('title')] # get_data------data filename = join(self.dir_path, 'mainContent_#%s_#%s.json' % (sd, ed)) # change file name url2 = 'https://e.yunos.com/api/rpt/list' payload = { 'param': json.dumps({ "page": 1, "page_size": 99999, "date_range_type": 0, "campaign_id": [i[0] for i in ids], "report_level": 1, "report_data_type": 1, "start_ds": sd, "end_ds": ed }), 'p_cf': p_cf } data = post(url2, data=payload, headers=headers) if not data.get('is_success'): return {'succ': False, 'msg': data.get('msg')} data = json.loads(data.get('msg').content.decode('utf-8')) if data.get('total_count') == 0: return {'succ': False, 'msg': 'no data', 'account': self.acc} with open(filename, 'w', encoding='utf-8') as f: json.dump(data, f) return {'succ': True, 'msg': data, 'ids': ids, 'sd': sd, 'ed': ed}
def get_vc(self): vc_path = settings.join(settings.IMG_PATH, 'vc.png') im = None try: element = self.d.find_element_by_xpath('//*[@id="frm-login"]//img') if u.cutimg_by_driver(self.d, element, vc_path): with open(vc_path, 'br') as i: im = i.read() vc_res = u.rc.rk_create(im, 3040) verify_code = vc_res.get('Result').lower() return verify_code, vc_res, im except: return self.get_vc()
def acc_list(): with open(join(BASEDIR, 'oppo_acct.json'), 'r', encoding='utf-8') as f: rd = json.load(f) # u = [{'id': num, 'platform': item[0], 'account': item[1], 'password': item[2]} for num, item in enumerate(rd, 1001)] u = [{ 'id': num, 'platform': i['platform'], 'account': i['account'], 'password': i['password'], 'dates': (2019, 7, 2019, 7) } for num, i in enumerate(rd, 1001)] # print(u) return u
def login(self, ui): self.d.get('http://channel.360.cn/') self.d.implicitly_wait(10) loginBtn = self.wait_element( By.CSS_SELECTOR, '._1wpszmp05gC2wm2vtV-s3d>div a:nth-child(2)') loginBtn.click() inpUser = self.wait_element(By.CSS_SELECTOR, 'input[name="account"]') inpUser.clear() inpUser.send_keys(ui['account']) inpPass = self.d.find_element_by_css_selector('input[name="password"]') inpPass.clear() inpPass.send_keys(ui['password']) time.sleep(2) im, lk_obj = None, None try: # vcodeimg = self.wait_element(By.CSS_SELECTOR, '.quc-captcha-img', timeout=5) vcodeimg = self.d.find_element_by_css_selector('.quc-captcha-img') vcimgpath = join(IMG_PATH, 'app_imgs', 'channel360FatherVerifyCode.png') u.cutimg_by_driver(self.d, vcodeimg, vcimgpath) with open(vcimgpath, 'rb') as f: im = f.read() # lk = u.rc.rk_create(im, '2050').get('Result') lk_obj = u.rc.rk_create(im, '3000') lk = lk_obj.get('Result').lower() inpVc = self.d.find_element_by_css_selector('.quc-input-captcha') inpVc.send_keys(lk) # time.sleep(8) except Exception as e: self.logger.error('login|%s' % e) btnLogin = self.d.find_element_by_css_selector('input[value="登录"]') btnLogin.click() time.sleep(6) loginRes = self.isLogin() self.logger.info('isLogin:%s' % loginRes) if loginRes == False: if lk_obj: u.rc.rk_report_error(lk_obj.get('Id')) return {'succ': False, 'msg': 'login fail'} else: if im: u.rc.rk_report(im, 3000, lk, vc_type=ui.get('platform')) return { 'succ': True, 'cookies': self.d.get_cookies(), 'appdict': loginRes }
def getData(self, cookie, osd, oed): ckstr = self.geneStr(cookie) esres = self.extractSel() logger.info('getData|extractSel: %s' % esres) for app in esres.get('appDict'): appName = esres.get('appDict').get(app) for tp in esres.get('typeDict'): tpName = esres.get('typeDict').get(tp) url = 'http://cpbi.ijinshan.com/?begin=%s&app=%s&end=%s&type=%s&c=user&a=' % ( osd, app, oed, tp) self.d.get(url) self.wait_element(By.CSS_SELECTOR, 'tr.thead td') theadtds = self.d.find_elements_by_css_selector('tr.thead td') channels = [] for theadtd in theadtds: if theadtd.text == '日期' or theadtd.text == '每日激活量': continue channels.append(theadtd.text) logger.info('getData|列表渠道:%s', channels) dataths = self.d.find_elements_by_css_selector( 'tr[style="background: #fff"]') datas = [] for datath in dataths: rowtds = datath.find_elements_by_tag_name('td') if len(rowtds) != 4: continue tmp = {} tmp['date'] = rowtds[0].text tmp['dayActivate'] = rowtds[1].text tmp[channels[0]] = rowtds[2].text tmp[channels[1]] = rowtds[3].text datas.append(tmp) dat = {'datas': datas, 'channels': channels} filepath = join( self.dir_path, '%s_%s_%s_%s.json' % (osd, oed, appName, tpName)) with open(filepath, 'w', encoding='utf-8') as f: f.write(json.dumps(dat, ensure_ascii=False)) logger.info('getData|写入文件成功:%s' % filepath) return { 'succ': True, 'data': { 'appDict': esres.get('appDict'), 'typeDict': esres.get('typeDict') } }
def save_balance_to_xls(self, header: list, data: list): # open workbook date = time.strftime('%Y-%m-%d') dir_name = settings.join(settings.sd_path, 'balance_data') if not os.path.exists(dir_name): os.makedirs(dir_name) file_name = settings.join(dir_name, f'account_balance_%s.xlsx' % date) if os.path.exists(file_name): wb = load_workbook(filename=file_name) else: wb = workbook.Workbook() # open worksheet sheet_names = wb.sheetnames if self.platform in sheet_names: ws = wb[self.platform] else: ws = wb.create_sheet(title=self.platform) if 'Sheet' in sheet_names: del wb['Sheet'] # write header if not ws['A1'].value: for col, i in enumerate(header, 1): ws.cell(row=1, column=col, value=i) # resort read_header = [x.value for x in next(ws.rows)] max_col = ws.max_column for c in header: if c not in read_header: max_col += 1 ws.cell(1, max_col, value=c) for k, v in enumerate(data.copy()): data[k] = [v.get(x, 0) for x in read_header] # write for i in data: ws.append(i) # save wb.save(file_name) return
def get_img(self, p_list, sd, ed): """截图,并处理图片文件""" with open(join(JS_PATH, 'e_qq_pagenum.js'), 'r') as p: pjs = p.read() for e in p_list: if not e.get('has_data'): continue picname = '%(productId)s_%(productName)s_%(sd)s_%(ed)s.png' % { 'productId': e.get('pid'), 'productName': e.get('pname'), 'sd': sd, 'ed': ed } url = 'https://e.qq.com/atlas/%s/report/order?ptype=20&pid=%s&pname=%s' % ( self.uid, e.get('pid'), quote(e.get('pname'))) self.d.get(url) time.sleep(0.5) if page_version == 'new': # 版本判断 try: self.wait_element(By.CLASS_NAME, 'button-more').click() except: self.d.execute_script( "document.querySelector('.button-more').click()") else: self.wait_element(By.LINK_TEXT, '查看报表', ec=EC.presence_of_element_located).click() time.sleep(2) # if page_version != 'new': # u.pag.hotkey('ctrl', '-', interval=0.3) # 调整分页数量 self.d.execute_script(pjs) time.sleep(1.5) self.d.switch_to.frame( self.d.find_element_by_css_selector( '.splitview-tabs-body iframe')) # 获取高度 get_height = 'return a=document.querySelector("#content").offsetHeight' height = self.d.execute_script(get_height) # 截图 cut_res = cut_img(height, self.dir_path, picname) if not cut_res['succ']: logger.error('get img %s_%s failed-------msg: %s' % (e['pid'], e['pname'], cut_res['msg'])) logger.info('height: %s ---picname: %s' % (height, picname)) # 恢复 # u.pag.hotkey('ctrl', '0', interval=0.3) else: return {'succ': True}
def login(self, ui): self.d.get('http://cpbi.ijinshan.com') time.sleep(1) inpAccount = self.wait_element(By.CSS_SELECTOR, '#username') inpAccount.clear() inpAccount.send_keys(ui['account']) time.sleep(1) inpPassword = self.d.find_element_by_css_selector( 'input[name="password"]') inpPassword.clear() inpPassword.send_keys(ui['password']) time.sleep(1) vcimgpath = join(IMG_PATH, 'app_imgs', 'liebaoVerifyCode.png') vcodeimg = self.d.find_element_by_id('code_img') u.cutimg_by_driver(self.d, vcodeimg, vcimgpath) lk, lk_obj, im = self.realizeCode(vcimgpath) if lk == None or len(lk) == 0: logger.error('识别错误') return False inpVcode = self.d.find_element_by_id('checkcode') inpVcode.send_keys(lk) time.sleep(3) # time.sleep(5) try: btnLoginBtn = self.wait_element(By.CSS_SELECTOR, '.login_submit', wait_time=3) btnLoginBtn.click() except Exception as e: try: text = self.d.find_element_by_id('err_checkcode').text if text == '验证码错误': logger.info('login|step1 error:验证码错误') u.rc.rk_report_error(lk_obj.get('Id')) finally: return False try: btnLogout = self.wait_element(By.CSS_SELECTOR, 'a[href="?a=logout"]', wait_time=10) logger.info('login|succ, cookie:%s' % self.d.get_cookies()) u.rc.rk_report(im, 3040, lk, vc_type=ui.get('platform')) return {'succ': True, 'cookies': self.d.get_cookies()} except Exception as e: logger.info('login|step2 error:没有登录成功') return False