py导出时段报表
作者:互联网
py导出时段报表
昨天做的任务,一天下来各种杂事,白天也时不时摸鱼,搞到晚上回家继续弄。今早弄些细节基本算完成~~~
这个脚本跟之前的最大区别,进去报表导出页面后,条件选择需要先点击下拉框,再选择地市或导出格式;另外一点,选择时间查询(前一天)的时候,先要抹掉默认值(用clear方法)再填入昨天的时间。
因为正值上班时间,赶紧发完博,继续搬砖。。
1 # -*- coding: utf-8 -*- 2 # @Time : 2022/4/19 8:57 3 # @Author : ljy 4 # @File : period-20220419.py 5 # 6 7 ''' 8 此脚本利用获取登陆系统的cookie,进去xx市销量页面,导出数据 9 10 ''' 11 12 # coding:utf-8 13 import os 14 from requests_toolbelt.multipart.encoder import MultipartEncoder 15 from selenium.webdriver.chrome.options import Options 16 from selenium import webdriver 17 from selenium.webdriver.common.action_chains import ActionChains 18 import time 19 import shutil 20 import datetime 21 import re 22 import requests 23 import json 24 from tkinter import * 25 import tkinter as tk 26 import tkinter.messagebox 27 28 from selenium.webdriver.support import expected_conditions as EC 29 from selenium.webdriver.common.by import By 30 from selenium.webdriver.support.wait import WebDriverWait 31 from selenium.webdriver.common.keys import Keys 32 import os 33 import requests 34 import sys 35 import shutil 36 37 38 ######################################################### 39 # 1、发送消息的函数 40 def send_msg(msg): 41 """ 42 :param msg: 要发送的消息 43 :return: 200 or False 44 """ 45 url = 'xxx' 46 program = {"msgtype": "text", "text": {"content": msg}, "at": {"atMobiles": ['我的手机号'], "isAtAll": "false"}} 47 headers = {'Content-Type': 'application/json'} 48 try: 49 f = requests.post(url, data=json.dumps(program), headers=headers) 50 except Exception as e: 51 return False 52 return f.status_code 53 54 55 ######################################################### 56 # 2、获取上个工作日时间 57 def last_workday(): 58 for i in range(1, 7): 59 now_time = datetime.datetime.now() 60 yes_time = now_time + datetime.timedelta(days=-i) 61 yes_time_week = yes_time.strftime('%Y-%m-%d') 62 global last_workdays 63 last_workdays = yes_time_week 64 return last_workdays 65 66 67 ######################################################### 68 # 3、 初始化浏览器 69 def browser_page(): 70 path_get = 'E:\\table' 71 print("当前路径: %s" % path_get) 72 time.sleep(3) 73 options = webdriver.ChromeOptions() 74 prefs = {'download.default_directory': path_get, 75 # prefs = {'download.default_directory': 'E:\\table', 76 'download.prompt_for_download': False, 77 "download.directory_upgrade": False, 78 "safebrowsing.enabled": False 79 } 80 options.add_experimental_option('prefs', prefs) 81 try: 82 global browser 83 browser = webdriver.Chrome(r"chromedriver", chrome_options=options) 84 except: 85 print("browser faild!") 86 send_msg("test: 浏览器初始化错误!") 87 browser = webdriver.Chrome(r"chromedriver") 88 89 ################################################################## 90 # 4、登录主页 91 def browser_project(browser): 92 # 设置cookies前必须访问一次页面 93 browser.get("某系统url") 94 try: 95 with open("cookies.txt", "r") as fp: 96 cookies = json.load(fp) 97 for cookie in cookies: 98 browser.add_cookie(cookie) 99 except: 100 print("获取cookie失败") 101 send_msg("test: cookie已过期,获取cookie失败") 102 # 拿cookie进去主页 103 browser.get("某系统url") 104 105 106 ############################################################## 107 # 5、进入主页跳转 108 def get_bo(browser): 109 wait = WebDriverWait(browser, 10, 0.5) 110 time.sleep(2) 111 # 5.1 点击“某关键字” 112 input_user = browser.find_elements_by_xpath(r'//*[@id="bodyer"]/div/div[2]/div/div[2]') 113 114 try: 115 input_user[0].click() 116 except IndexError: 117 send_msg("test:cookie文件失效,请重新执行!") 118 tk.Tk().withdraw() 119 tk.messagebox.showinfo("提示", "请先运行: 获取cookie脚本") 120 browser.quit() 121 # exit() 122 123 time.sleep(5) 124 browser.switch_to.window(browser.window_handles[1]) 125 # 5.2 选择“数据报表” 126 input_user = browser.find_elements_by_xpath(r'//*[@id="mainNav"]/li[3]/a') 127 128 time.sleep(5) 129 input_user[0].click() 130 131 # 5.3 选择“xxx时段报表” 132 input_user = browser.find_elements_by_xpath( 133 r'//*[@id="ctl00_ctl00_content_ContentPlaceHolder2_uc_report1_divReportContent"]/div[6]/ul/li[2]/p/a') 134 time.sleep(2) 135 input_user[0].click() 136 browser.switch_to.window(browser.window_handles[2]) 137 138 ################################################################## 139 140 # 6、销量时段报表页面,导出数据 141 142 # 6.1 选择时间: 143 # 报错,下面写法才对 144 # browser.find_element_by_id(r'//*[@id="reportViewer_ctl04_ctl03_txtValue"]').clear() 145 # browser.find_elements_by_xpath(r'//*[@id="reportViewer_ctl04_ctl03_txtValue"]').clear() 146 147 ## 6.1.1 左边日期框 148 last_workdays = last_workday() 149 print(last_workdays) 150 time.sleep(20) 151 152 browser.find_element_by_id("reportViewer_ctl04_ctl03_txtValue").clear() 153 browser.find_element_by_id('reportViewer_ctl04_ctl03_txtValue').send_keys(last_workdays) 154 155 ## 6.1.2 右边日期框 156 time.sleep(2) 157 browser.find_element_by_id("reportViewer_ctl04_ctl05_txtValue").clear() 158 browser.find_element_by_id('reportViewer_ctl04_ctl05_txtValue').send_keys(last_workdays) 159 160 # 6.2 选择地市: 161 ## 6.2.1 先点下拉 162 DownButton = browser.find_elements_by_xpath(r'//*[@id="reportViewer_ctl04_ctl07_ddDropDownButton"]') 163 time.sleep(2) 164 DownButton[0].click() 165 166 ## 6.2.2 点“全选” 167 QuanXuan = browser.find_elements_by_xpath(r'//*[@id="reportViewer_ctl04_ctl07_divDropDown_ctl00"]') 168 time.sleep(2) 169 QuanXuan[0].click() 170 171 ## 6.2.3 点“顺德” 172 ShunDe = browser.find_elements_by_xpath(r'//*[@id="reportViewer_ctl04_ctl07_divDropDown_ctl23"]') 173 time.sleep(2) 174 ShunDe[0].click() 175 176 # 6.3 点击“查看报表” 177 time.sleep(5) 178 input_user = browser.find_elements_by_xpath(r'//*[@id="reportViewer_ctl04_ctl00"]') 179 input_user[0].click() 180 181 time.sleep(10) 182 ## 6.3.1 先点下拉 183 buttonLink = browser.find_element_by_xpath(r'//*[@id="reportViewer_ctl06_ctl04_ctl00_ButtonLink"]') 184 time.sleep(2) 185 buttonLink.click() 186 187 ## 导出为excel 188 menu = browser.find_elements_by_xpath(r'//*[@id="reportViewer_ctl06_ctl04_ctl00_Menu"]/div[5]/a') 189 time.sleep(2) 190 menu[0].click() 191 192 # 6.4 报表改名 193 path_get = os.getcwd() 194 filepath2 = change_name(path_get, "period tb") 195 196 browser.quit() 197 print("exit") 198 199 ########################### 200 # 修改导出报表的名字 201 def change_name(url, add): 202 url = 'E:\\table' 203 print("改名,当前路径: %s" % url) 204 time.sleep(3) 205 print(r"RF_0202%s" % last_workdays) 206 patt = re.compile(r"RF_0202") 207 lists = os.listdir(url) 208 print(lists) 209 while True: 210 lists.sort(key=lambda fn: os.path.getmtime(url + '\\' + fn)) 211 filepath = os.path.join(url, lists[-1]) 212 try: 213 context = patt.search(filepath).group() 214 if context: 215 break 216 except: 217 try: 218 lists.pop() 219 except: 220 send_msg("test: %s -- 报表导出失败,没有文件!" % add) 221 # ok 222 filepath2 = url + "\\" + add + "-" + last_workdays.replace("-", "") + ".xls" 223 224 print(filepath2) 225 time.sleep(5) 226 227 shutil.move(filepath, filepath2) 228 return filepath2 229 # 230 # 231 # ###################### 导入报表 232 后续等公司开发弄完再做。。。 233 234 ################################################################# 235 def start_project_bo(): 236 # last_workday() 237 browser_page() 238 browser_project(browser) 239 get_bo(browser) 240 241 if __name__ == '__main__': 242 start_project_bo()
大家主要看 “6、销量时段报表页面,导出数据”,前面都是各种页面跳转到报表页面。
这个模板,以后可以继续参考用
成果图:
标签:报表,py,导出,find,sleep,time,import,id,browser 来源: https://www.cnblogs.com/windysai/p/16168581.html