def login_ffp(driver, username=username, password=password): while True: driver.get(ffp_url) driver.implicitly_wait(10) try: driver.find_element_by_id('acceptButton').click() except: pass username_input = driver.find_element_by_id('j_username') password_input = driver.find_element_by_id('j_password') captcha_input = driver.find_element_by_id('j_verify') username_input.clear() driver.implicitly_wait(1) username_input.send_keys(username) driver.implicitly_wait(1) password_input.send_keys(password) driver.implicitly_wait(1) captcha_image_url = captcha_input.find_element_by_xpath( '..').find_element_by_tag_name('img').get_attribute('src') captcha_code = get_captcha_image(captcha_image_url) captcha_input.send_keys(captcha_code) driver.implicitly_wait(1) driver.find_element_by_id('loginButton').click() driver.implicitly_wait(10) if 'error' in driver.current_url: choice = yes_no_box('登录失败,是否重新登录?', 'ICS登录') if choice == '是': continue else: alert_box('您正在退出程序,感谢使用', '退出程序') os._exit(0) driver.find_element_by_id('row_customer').click() driver.implicitly_wait(10) return driver
def main(): start_date = date_box('请输入开始日期', '开始日期') end_date = date_box('请输入结束日期', '结束 日期') date_list = create_date_list(start_date, end_date) if not date_list: alert_box('日期填写有误', '错误') os._exit(0) st_data = get_st_data(date_list, start_date, end_date) sales_data = get_sales_data(start_date, end_date) data = compare_data(st_data, sales_data) describe_data(data)
def open_app(app_path): try: os.startfile(app_path) time.sleep(3) keyboard_press('enter') time.sleep(3) keyboard_press('enter') return True except: alert_box('无法打开应用程序%s,请检查程序路径是否正确' % app_path, '打开应用程序失败') os._exit(0)
def get_sales_data(start_date, end_date): if '%s_%s_sales_total.csv' % (start_date, end_date) in os.listdir(sales_dir): try: sales_data = pd.read_csv('%s%s%s_%s_sales_total.csv' % (sales_dir, os.sep, start_date, end_date), encoding='gbk', converters={'关联票号': str}) except: sales_data = pd.read_csv('%s%s%s_%s_sales_total.csv' % (sales_dir, os.sep, start_date, end_date), converters={'关联票号': str}) return sales_data sales_data = pd.DataFrame(None) for root, _, files in os.walk(sales_dir): for file in files: sales_data_temp = pd.read_excel(os.path.join(root, file), skiprows=3, converters={'关联票号': str}) if sales_data_temp.shape[0] <= 4: continue sales_data_temp.drop(sales_data_temp.head(2).index, inplace=True) # CAN # sales_data_temp = sales_data_temp[(sales_data_temp['起飞机场'] == 'CAN') & (sales_data_temp['销售单位'] == 'SMARTSERVICE系统') & (sales_data_temp['下单渠道'] == 'SS') & (sales_data_temp['EMD类型'].isin(['柜台升舱', '一人多座', '休息室升舱', '机场柜台升舱']) == True)] # URC sales_data_temp = sales_data_temp[ (sales_data_temp['起飞机场'] == 'URC') & (sales_data_temp['销售单位'] == 'SMARTSERVICE系统') & (sales_data_temp['下单渠道'] == 'SS') & (sales_data_temp['EMD类型'].isin( ['柜台升舱', '一人多座', '休息室升舱', '机场柜台升舱']) == True)] sales_data = pd.concat([sales_data, sales_data_temp], ignore_index=True, join='outer') if sales_data.shape[0] == 0: alert_box('未提取到销售数据', '错误') os._exit(0) sales_data.fillna('', inplace=True) sales_data = sales_data[['EMD类型', '关联票号', '支付价', '扣减里程/配额', '订单下单人姓名']] sales_data.to_csv('%s%s%s_%s_sales_total.csv' % (sales_dir, os.sep, start_date, end_date)) return sales_data
def get_ff_list(date): try: ff_list = pd.read_excel('%s%s%s.xlsx' % (ics_processed_dir, os.sep, date), converters={ 'flt_num': str, 'ff_number': str }) except: alert_box( '未找到当天航班的xlsx文档,请检查。注:文档需从SOC预报中直接复制入Excel表格,并以YYYY-MM-DD_XXX.xlsx命名,其中XXX为场站三字码。', '退出程序') os._exit(0) ff_list = ff_list[(ff_list['ff_airline'] == 'CZ') & (ff_list['ff_number'].isna() == False) & (ff_list['ff_number'] != '')] if len(ff_list) == 0: alert_box('会员列表无数据,请确认。', '退出程序') os._exit(0) ff_list.fillna('', inplace=True) return ff_list
def check_file(file): if not os.path.exists(file): alert_box('缺少 %s 文件' % file, '错误') os._exit(0)
def get_st_data(date_list, start_date, end_date): if '%s_%s_st_total.csv' % (start_date, end_date) in os.listdir(task_dir): try: st_data = pd.read_csv('%s%s%s_%s_st_total.csv' % (task_dir, os.sep, start_date, end_date), encoding='gbk', converters={ '会员卡号': str, '飞行日期': str, 'OC航班号': str, '电子客票号': str }) except: st_data = pd.read_csv('%s%s%s_%s_st_total.csv' % (task_dir, os.sep, start_date, end_date), converters={ '会员卡号': str, '飞行日期': str, 'OC航班号': str, '电子客票号': str }) return st_data st_data = pd.DataFrame(None) files = os.listdir(task_dir) for date in date_list: for file in files: if date in file: try: st_data_temp = pd.read_csv('%s%s%s' % (task_dir, os.sep, file), encoding='gbk', converters={ '会员卡号': str, '飞行日期': str, 'OC航班号': str, '电子客票号': str }) except: st_data_temp = pd.read_csv('%s%s%s' % (task_dir, os.sep, file), converters={ '会员卡号': str, '飞行日期': str, 'OC航班号': str, '电子客票号': str }) if st_data_temp.shape[0] != 0 and st_data_temp is not None: st_data_temp.replace('\t', '', regex=True, inplace=True) st_data_temp['飞行日期'] = pd.to_datetime( st_data_temp['飞行日期']).dt.strftime('%Y-%m-%d') st_data_temp.fillna('', inplace=True) # CAN # st_data_temp = st_data_temp[st_data_temp['航段始发机场'] == 'CAN'] # URC st_data_temp = st_data_temp[st_data_temp['航段始发机场'] == 'URC'] st_data = pd.concat([st_data, st_data_temp], ignore_index=True, join='outer') else: continue if st_data.shape[0] == 0: alert_box('未提取到st数据', '错误') os._exit(0) st_data.fillna('', inplace=True) st_data = st_data[[ 'UID', '航段', '飞行日期', '航段性质', '会员级别', 'OC母舱位', '电子客票号', '查询', '备注' ]] st_data.to_csv('%s%s%s_%s_st_total.csv' % (task_dir, os.sep, start_date, end_date)) return st_data
'flt_date', 'station', 'flt_num', 'name', 'seat_class', 'group_', 'ff_number', 'ff_level', 'available_miles', 'inbound' ]) structured_data.to_excel('%s%s%s_ffp.xlsx' % (ics_results_dir, os.sep, date)) def logout_ffp(driver): driver.find_element_by_id('arrow').click() driver.implicitly_wait(2) driver.find_element_by_id('logout').click() driver.implicitly_wait(2) if __name__ == "__main__": date = get_date() ff_list = get_ff_list(date) try: driver = open_driver() login_ffp(driver) unstructured_data = main_crawler(driver, ff_list) data_structuring(unstructured_data, date) alert_box('数据处理完毕,请从%s文件夹查看,感谢使用!' % ics_results_dir, '退出程序') except: alert_box('程序出现问题,正在退出程序,感谢使用!', '退出程序') finally: logout_ffp(driver) driver.quit()