Browse Source

Merge branch 'nene_w' into debug

zwxi 1 year ago
parent
commit
e0cd6990a6

+ 79 - 8
eta_report2img/html2img_ping.py

@@ -3,6 +3,8 @@ from time import sleep
 from selenium import webdriver
 from selenium.webdriver.chrome.service import Service
 import img_pdf_png
+import os
+from PIL import Image
 
 
 def scroll_page(driver):
@@ -19,7 +21,7 @@ def scroll_page(driver):
         for j in range(n1):
             # print(r"j:{}".format(j))
             if j == n1 - 1 and last_height > 0:  # 截取尾部
-                driver.execute_script("window.scrollBy(0,{})".format(page_height))  # 滚动条滚到底
+                driver.execute_script("window.scrollBy(0,{})".format(last_height))  # 滚动条滚到底
                 print("滚到底了")
             else:
                 driver.execute_script("window.scrollBy(0,{})".format(web_height))
@@ -45,26 +47,95 @@ def html2img(driver, image_folder):
     print("当前窗口网页高度{}".format(web_height))
     last_height = page_height % web_height
     print("底部多出来的部分{}".format(last_height))
-    margin_flag = False
+
+    is_end = 0
     if n1 == 0 or (n1 == 1 and last_height == 0):  # 判断是否需要滚动
-        driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0'))  # 指定截图保存位置
+        # 去除留白部分, 一般情况下也不会只写一页报告且留那么多空白
+        crop_height = web_height - page_height
+        if crop_height > 0:
+            origin_last_img = r'{}/{}.png'.format(image_folder, 1000)
+            new_last_img = r'{}/{}.png'.format(image_folder, 0)
+            driver.save_screenshot(origin_last_img)
+            crop_img_botton(origin_last_img, new_last_img, page_height)
+        else:
+            driver.get_screenshot_as_file(r'{}/{}.png'.format(image_folder, '0'))
     else:
         j = 0
         while j <= n1:
             if j == 0:
                 driver.execute_script("window.scrollBy(0,0)")
                 # print(0)
-            elif j == n1 and last_height > 0:  # 截取尾部
-                driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
-                driver.execute_script("window.scrollBy(0,{})".format(web_height))  # 滚动条滚到底
+            elif j == n1:
+                # print(f'n1 is {n1}')
+                if last_height > 0:
+                    # driver.execute_script("$('#app #resetcss').css('margin-bottom', '{}px')".format(web_height))
+                    driver.execute_script("window.scrollBy(0,{})".format(last_height))  # 滚动条滚到底
+
+                    # 最后一屏先进行一次保存
+                    sleep(1)
+                    origin_last_img = r'{}/{}.png'.format(image_folder, j + 1000)
+                    driver.save_screenshot(origin_last_img)
+
+                    # 截取上面重叠的一部分并保存截取后的图片
+                    new_last_img = r'{}/{}.png'.format(image_folder, j)
+                    crop_height = web_height - last_height
+                    crop_img_top(origin_last_img, new_last_img, crop_height)
+
+                is_end = 1
                 print("拉到底拉")
             else:
                 driver.execute_script("window.scrollBy(0,{})".format(web_height))
-            sleep(1)
-            driver.save_screenshot(r'{}/{}.png'.format(image_folder, j))  # 截屏
+
+            if is_end == 0:
+                sleep(1)
+                driver.save_screenshot(r'{}/{}.png'.format(image_folder, j))  # 截屏
             j = j + 1
 
 
+# crop_img_top 裁掉图片上面的部分保留剩下的部分
+def crop_img_top(image_path, output_path, crop_height):
+    # 打开图像文件
+    img = Image.open(image_path)
+
+    # 获取图像的宽度和高度
+    width, height = img.size
+
+    # 计算裁剪的起始坐标
+    start_x = 0
+    start_y = min(height, crop_height)  # 保证不超出图像下边界
+
+    # 裁剪图像,仅保留上半部分
+    cropped_img = img.crop((start_x, start_y, start_x + width, start_y + (height - crop_height)))
+
+    # 保存裁剪后的图像
+    cropped_img.save(output_path)
+
+    # 删除原始图像
+    os.remove(image_path)
+
+
+# crop_img_botton 裁掉图片下面的部分保留上面的部分
+def crop_img_botton(image_path, output_path, keep_height):
+    # 打开图像文件
+    img = Image.open(image_path)
+
+    # 获取图像的宽度和高度
+    width, height = img.size
+
+    # 计算保留部分的起始坐标
+    start_x = 0
+    start_y = 0
+
+    # 裁剪图像,保留从左上角开始指定高度的部分
+    cropped_img = img.crop((start_x, start_y, start_x + width, start_y + keep_height))
+
+    # 保存裁剪后的图像
+    cropped_img.save(output_path)
+
+    # 删除原始图像
+    os.remove(image_path)
+
+
 # 调用截图函数
 if __name__ == "__main__":
     # 创建一个 Chrome WebDriver 实例

+ 2 - 5
eta_report2img/img_pdf_png.py

@@ -35,13 +35,10 @@ def merge_images(image_folder, output_file, file_name, output_type, n=1):
     now_height = 0
     for i in range(image_count):
         img = Image.open(os.path.join(image_folder, target_img[i]))
-        img_size0 = int(img.size[0] / n)
-        img_size1 = int(img.size[1] / n)
-        img = img.resize((img_size0, img_size1))
-        if i != 0:
-            now_height += img.size[1]
         # 纵向拼接图片
         new_img.paste(img, (0, now_height))
+        # 当前粘贴高度自增已拼接的图片高度
+        now_height += img.size[1]
 
     # 默认两种类型都生成
     if output_type == "":

+ 1 - 1
eta_report2img/main.py

@@ -85,7 +85,7 @@ def create_img_and_pdf(report_url, file_name, output_type=""):
     options.add_argument("disable-extensions")
     options.add_argument("headless")  # 无头浏览器模式
     s = Service(executable_path=chrome_drive_path)
-    driver = webdriver.Chrome(service=s, options=options)
+    driver = webdriver.Chrome(options=options)
 
     # 加载页面
     print("加载页面")

+ 0 - 0
html2img/html/a.txt


+ 0 - 0
html2img/img/b.txt


+ 135 - 0
html2img/main.py

@@ -0,0 +1,135 @@
+# coding=utf-8
+import flask
+import imgkit
+import json
+import os
+import oss2
+import random
+from flask import request
+
+# 创建一个服务,把当前这个python文件当做一个服务
+server = flask.Flask(__name__)
+
+# 端口配置
+port = 5008
+host = '127.0.0.1'
+debug = True
+
+# 工具配置
+tool_path = r'/usr/local/bin/wkhtmltoimage'
+# tool_path = r'E:\wkhtmltopdf\bin\wkhtmltoimage.exe'
+
+# OSS配置
+oss_end_point = 'oss-cn-shanghai.aliyuncs.com'
+oss_bucket = 'hzchart'
+oss_key_id = 'LTAIFMZYQhS2BTvW'
+oss_key_secret = '12kk1ptCHoGWedhBnKRVW5hRJzq9Fq'
+resource_host = 'https://hzstatic.hzinsights.com/'
+
+
+# server.config['JSON_AS_ASCII'] = False
+# @server.route()可以将普通函数转变为服务 登录接口的路径、请求方式
+@server.route('/htm2img', methods=['post'])
+def htm2img():
+    # 获取参数
+    req_data = request.get_json()
+    if req_data is None:
+        return json.dumps({'code': 5004, 'msg': '参数有误', 'data': ''}, ensure_ascii=False)
+
+    # 参数校验
+    contents = req_data['html_content']
+    if contents is None or contents == '':
+        return json.dumps({'code': 5004, 'msg': '获取html文本内容失败', 'data': ''}, ensure_ascii=False)
+    width = req_data['width']
+    if width <= 0:
+        return json.dumps({'code': 5004, 'msg': '图片宽度有误', 'data': ''}, ensure_ascii=False)
+    height = req_data['height']
+    if height <= 0:
+        return json.dumps({'code': 5004, 'msg': '图片长度有误', 'data': ''}, ensure_ascii=False)
+
+    # 生成html文件
+    rand_name = get_rand_string()
+    html_path = 'html/' + rand_name + '.html'
+    res = html2file(contents, html_path)
+    if not res:
+        return json.dumps({'code': 5004, 'msg': '生成html失败', 'data': ''}, ensure_ascii=False)
+
+    # html转图片
+    img_name = rand_name + '.jpg'
+    img_path = html_to_img(html_path, img_name, width, height)
+    if img_path == '':
+        return json.dumps({'code': 400, 'msg': '生成图片失败', 'data': ''}, ensure_ascii=False)
+
+    # 上传OSS
+    upload_dir = 'static/images/yb/htm2img/'
+    save_path = upload_dir + img_name
+    upload_res = upload_oss_file(save_path, img_path)
+
+    # 清除本地生成的html及图片
+    clear_local_file(img_path, html_path)
+
+    if upload_res.status == 200:
+        resource_url = resource_host + save_path
+        return json.dumps({'code': 200, 'msg': '生成图片成功', 'data': resource_url}, ensure_ascii=False)
+    else:
+        return json.dumps({'code': 400, 'msg': '生成图片失败', 'data': ''}, ensure_ascii=False)
+
+
+# html文件转为图片
+def html_to_img(html_url, img_name, width=100, height=100):
+    path_wkimg = tool_path  # 工具路径
+    cfg = imgkit.config(wkhtmltoimage=path_wkimg)
+    opt = {"width": width, "height": height, "quality": 100, "format": "jpg"}
+
+    img_path = 'img/' + img_name
+    imgkit.from_file(html_url, img_path, config=cfg, options=opt)
+    try:
+        os.path.isfile(img_path)
+        return img_path
+    except Exception as e:
+        # print(e)
+        return ''
+
+
+# 上传图片至OSS
+def upload_oss_file(save_path, file_path):
+    auth = oss2.Auth(oss_key_id, oss_key_secret)
+    bucket = oss2.Bucket(auth, oss_end_point, oss_bucket)
+    return bucket.put_object_from_file(save_path, file_path)
+
+
+# 生成指定长度的随机字符串
+def get_rand_string(size=28):
+    random_str = ''
+    base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'
+    length = len(base_str) - 1
+    for i in range(size):
+        random_str += base_str[random.randint(0, length)]
+    return random_str
+
+
+# 清除本地生成的文件
+def clear_local_file(img_path, html_path):
+    try:
+        os.remove(img_path)
+        os.remove(html_path)
+        return True
+    except OSError as e:
+        # print(e)
+        return False
+
+
+# html文本生成文件
+def html2file(content, file_path):
+    try:
+        f = open(file_path, 'w', encoding='UTF-8')
+        f.write(content)
+        f.close()
+        return True
+    except Exception as e:
+        # print(e)
+        return False
+
+
+if __name__ == '__main__':
+    server.run(debug=debug, port=port, host=host)

+ 12 - 0
html2img/工具说明.txt

@@ -0,0 +1,12 @@
+# 工具安装
+	https://wkhtmltopdf.org/downloads.html
+# 工具测试
+	https://blog.csdn.net/weixin_45019350/article/details/115799676
+
+# flask部署参考
+	https://blog.csdn.net/qq_50258800/article/details/108164175
+	https://blog.csdn.net/qq_43580193/article/details/118523117
+
+# main.py配置修改
+	main.py:12	端口配置
+	main.py:18	工具path

+ 47 - 0
lt_api.py

@@ -0,0 +1,47 @@
+import eikon as ek
+
+import hug
+from bottle import route, run
+import json
+import pandas as pd
+import urllib.parse
+
+hug.API(__name__).http.output_format = hug.output_format.json
+
+# 填写路透的key
+key = ""
+
+
+@hug.get('/hz_server')
+def hello():
+    return 'ek true'
+
+
+@hug.get('/edbInfo/ek')
+def GetEdbDataFromEk(EdbCode, StartDate, EndDate):
+    EdbCode = urllib.parse.unquote(EdbCode)
+    print("EdbCode")
+    print(EdbCode)
+    print(StartDate)
+    print(EndDate)
+    print("req start")
+    data = ek.get_timeseries(EdbCode, fields='*', start_date=StartDate, end_date=EndDate)
+    print("result start")
+    print(data)
+    print("是否唯一索引:", data.index.is_unique)
+    if  data.index.is_unique ==False:
+        print("开始去重")
+        data = data.groupby(data.index).first()
+        print("结束去重")
+        print(data)
+    
+    print("result end")
+    dataJson = data.to_json()
+    result = json.loads(dataJson)
+    return result
+
+
+if __name__ == "__main__":
+    ek.set_app_key(key)
+    app = __hug__.http.server()
+    run(app=app, reloader=True, port=7002)

+ 86 - 0
mail.py

@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+import poplib
+import datetime
+import time
+from email.parser import Parser
+from email.header import decode_header
+
+# 输入邮件地址, 口令和POP3服务器地址:
+email = ''
+password = '' # 注意使用开通POP,SMTP等的授权码
+pop3_server = 'pop.qiye.aliyun.com'
+
+
+def decode_str(s):  # 字符编码转换
+    value, charset = decode_header(s)[0]
+    if charset:
+        value = value.decode(charset)
+    return value
+
+
+def get_att(msg):
+    import email
+    attachment_files = []
+
+    for part in msg.walk():
+        file_name = part.get_filename()  # 获取附件名称类型
+        contType = part.get_content_type()
+        print(file_name)
+        if file_name:
+            h = email.header.Header(file_name)
+            dh = email.header.decode_header(h)  # 对附件名称进行解码
+            filename = dh[0][0]
+            if dh[0][1]:
+                filename = decode_str(str(filename, dh[0][1]))  # 将附件名称可读化
+                print(filename)
+                # filename = filename.encode("utf-8")
+            data = part.get_payload(decode=True)  # 下载附件
+            att_file = open('/home/code/python/coal_mail/emailFile/' + filename, 'wb')  # 在指定目录下创建文件,注意二进制文件需要用wb模式打开
+            attachment_files.append(filename)
+            att_file.write(data)  # 保存附件
+            att_file.close()
+    return attachment_files
+
+
+# 连接到POP3服务器,有些邮箱服务器需要ssl加密,对于不需要加密的服务器可以使用poplib.POP3()
+server = poplib.POP3_SSL(pop3_server)
+server.set_debuglevel(1)
+# 打印POP3服务器的欢迎文字:
+print(server.getwelcome().decode('utf-8'))
+# 身份认证:
+server.user(email)
+server.pass_(password)
+# 返回邮件数量和占用空间:
+print('Messages: %s. Size: %s' % server.stat())
+# list()返回所有邮件的编号:
+resp, mails, octets = server.list()
+# 可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
+print(mails)
+index = len(mails)
+print(index)
+for i in range(index, 0, -1):
+    # 倒序遍历邮件
+    resp, lines, octets = server.retr(i)
+    # lines存储了邮件的原始文本的每一行,
+    # 邮件的原始文本:
+    msg_content = b'\r\n'.join(lines).decode('utf-8')
+    # 解析邮件:
+    msg = Parser().parsestr(msg_content)
+    # 获取邮件时间
+    date1 = time.strptime(msg.get("Date")[0:24], '%a, %d %b %Y %H:%M:%S')  # 格式化收件时间
+    date2 = time.strftime("%Y%m%d", date1)  # 邮件时间格式转换
+    now_time = datetime.datetime.now()
+    # // 自定义下载附件时间范围
+    getFileDay = (now_time + datetime.timedelta(days=-1)).strftime("%Y%m%d")
+    # 邮件最早的时间
+    print(date1)
+    # 邮件最近的时间
+    print(date2)
+    if date2 < getFileDay:
+        continue
+    f_list = get_att(msg)  # 获取附件
+    # print_info(msg)
+
+server.quit()
+

+ 87 - 0
mail_xiangyu.py

@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+import poplib
+import datetime
+import time
+from email.parser import Parser
+from email.header import decode_header
+
+# 输入邮件地址, 口令和POP3服务器地址:
+email = ''
+password = '' # 注意使用开通POP,SMTP等的授权码
+pop3_server = 'pop3.xiangyu.cn'
+
+
+def decode_str(s):  # 字符编码转换
+    value, charset = decode_header(s)[0]
+    if charset:
+        value = value.decode(charset)
+    return value
+
+
+def get_att(msg):
+    import email
+    attachment_files = []
+
+    for part in msg.walk():
+        file_name = part.get_filename()  # 获取附件名称类型
+        contType = part.get_content_type()
+        print(file_name)
+        if file_name:
+            h = email.header.Header(file_name)
+            dh = email.header.decode_header(h)  # 对附件名称进行解码
+            filename = dh[0][0]
+            if dh[0][1]:
+                filename = decode_str(str(filename, dh[0][1]))  # 将附件名称可读化
+                print(filename)
+                # filename = filename.encode("utf-8")
+            data = part.get_payload(decode=True)  # 下载附件
+#             att_file = open('/home/code/python/coal_mail/emailFile/' + filename, 'wb')  # 在指定目录下创建文件,注意二进制文件需要用wb模式打开
+            att_file = open('/Users/xi/Desktop/coal_mail/emailFile/' + filename, 'wb')  # 在指定目录下创建文件,注意二进制文件需要用wb模式打开
+            attachment_files.append(filename)
+            att_file.write(data)  # 保存附件
+            att_file.close()
+    return attachment_files
+
+
+# 连接到POP3服务器,有些邮箱服务器需要ssl加密,对于不需要加密的服务器可以使用poplib.POP3()
+server = poplib.POP3_SSL(pop3_server,'995')
+server.set_debuglevel(1)
+# 打印POP3服务器的欢迎文字:
+print(server.getwelcome().decode('utf-8'))
+# 身份认证:
+server.user(email)
+server.pass_(password)
+# 返回邮件数量和占用空间:
+print('Messages: %s. Size: %s' % server.stat())
+# list()返回所有邮件的编号:
+resp, mails, octets = server.list()
+# 可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
+print(mails)
+index = len(mails)
+print(index)
+for i in range(index, 0, -1):
+    # 倒序遍历邮件
+    resp, lines, octets = server.retr(i)
+    # lines存储了邮件的原始文本的每一行,
+    # 邮件的原始文本:
+    msg_content = b'\r\n'.join(lines).decode('utf-8')
+    # 解析邮件:
+    msg = Parser().parsestr(msg_content)
+    # 获取邮件时间
+    date1 = time.strptime(msg.get("Date")[0:24], '%a, %d %b %Y %H:%M:%S')  # 格式化收件时间
+    date2 = time.strftime("%Y%m%d", date1)  # 邮件时间格式转换
+    now_time = datetime.datetime.now()
+    # // 自定义下载附件时间范围
+    getFileDay = (now_time + datetime.timedelta(days=-1)).strftime("%Y%m%d")
+    # 邮件最早的时间
+    print(date1)
+    # 邮件最近的时间
+    print(date2)
+    if date2 < getFileDay:
+        continue
+    f_list = get_att(msg)  # 获取附件
+    # print_info(msg)
+
+server.quit()
+

+ 202 - 0
rpa_mysteel_refresh.py

@@ -0,0 +1,202 @@
+import hug
+from bottle import route, run
+import pyautogui
+import random
+import time
+import os
+import win32gui, win32con
+import autoit
+import pyperclip
+import pythoncom
+import datetime
+
+hug.API(__name__).http.output_format = hug.output_format.json
+
+exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et"
+# exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
+# exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
+
+
+# 顶部钢联按钮的x,y位置
+gl_button_x = 645
+gl_button_y = 45
+
+# 钢联选项卡下面的刷新按钮的x,y位置
+gl_refresh_button_x = 302
+gl_refresh_button_y = 87
+
+# 获取数据的超时时间(单位:s)
+get_data_timeout_time = 30
+
+# 随机点击的按钮范围
+pointMinX = 200
+pointMaxX = 650
+pointMinY = 300
+pointMaxY = 550
+
+@hug.get('/mysteel_chemical/server')
+def mysteel_chemical_server():
+    return 1
+
+@hug.get('/mysteel_chemical/refresh')
+def mysteel_chemical_refresh(FilePath):
+    screen_width, screen_height = pyautogui.size()
+    print("屏幕分辨率:{} x {}".format(screen_width, screen_height))
+
+    pyautogui.FAILSAFE = False
+    # FilePath = urllib.parse.unquote(FilePath)
+    FilePath = FilePath.replace('"', '')
+
+    pyautogui.hotkey('win', 'd')
+    # time.sleep(2)
+    #
+    # moveX = 1100
+    # moveY = 600
+    # pyautogui.moveTo(moveX, moveY, 0.5)
+    # pyautogui.click(moveX, moveY)
+    try:
+        fileFullPath = FilePath
+        print("fileFullPath start")
+        # print(fileFullPath)
+
+        fileFullPath = fileFullPath.replace("\\\\", "\\")
+
+        # fileFullPath.replace("\\", "\\")
+        # fileFullPath.replace("ppp", "")
+
+        # print("fileFullPath")
+        print(fileFullPath)
+
+        # app = xw.App(visible=True, add_book=False)
+        # app.display_alerts = False
+        # app.screen_updating = True
+        # print("init app")
+        if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
+                and fileFullPath.find("~$") < 0 \
+                and fileFullPath.find("template") < 0:
+
+            # 获取文件名和扩展名
+            file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath))
+            file_name_ext = file_name + file_ext
+            print(file_name_ext)
+            print(datetime.datetime.now())
+
+            try:
+                # 打开文件
+                # pythoncom.CoInitialize()
+                # pyautogui.hotkey('win','r')
+                # pyautogui.sleep(0.5)
+                # time.sleep(2)
+                # pyperclip.copy(fileFullPath)
+                # pyautogui.hotkey('ctrl', 'v')
+                # pyautogui.keyDown('enter')
+                # pyautogui.keyUp('enter')
+                # time.sleep(5)
+                #
+                # # open full screen
+                # hwnd = win32gui.GetForegroundWindow()
+                # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
+                # time.sleep(1)
+
+                # 启动程序
+                code = exePath + " " + fileFullPath
+                # print(code)
+                autoit.run(code)
+                autoit.win_wait_active(file_name_ext, timeout=20)  # 等待窗口激活
+                autoit.win_move(file_name_ext, 0, 0, 1024, 768)
+                # autoit.win_move(file_name_ext, 0, 0, 1920, 1080)
+
+                print("监听成功了")
+
+            except Exception as e:
+                print("打开", fileFullPath, "失败:", e)
+
+                ## 失败的话,截图记录下原因
+                screenshot = pyautogui.screenshot()
+                imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S")+"_screenshot_img.png"
+                screenshot.save(imgPath)
+
+                hwnd = win32gui.GetForegroundWindow()
+                found_title = win32gui.GetWindowText(hwnd)
+                print("窗口名称:", found_title)
+                if found_title == "":
+                    print("窗口未打开")
+                    return
+                if found_title.startswith(file_name_ext) == False:
+                    print("窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)
+                    return
+                autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768)
+                # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080)
+
+
+            ### 随机2-3次点击 单元格区域(每次点击停留1s)
+            r = random.randint(2, 3)
+            for i in range(r):
+                pointX = random.randint(pointMinX, pointMaxX)
+                pointY = random.randint(pointMinY, pointMaxY)
+                print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
+                pyautogui.moveTo(pointX, pointY, 0.5)
+                pyautogui.click()
+                time.sleep(1)
+
+            print("start dataBtn")
+            pyautogui.moveTo(gl_button_x, gl_button_y, 0.5)
+            pyautogui.click()
+            time.sleep(2)
+            print("end dataBtn")
+
+            pointX = random.randint(pointMinX, pointMaxX)
+            pointY = random.randint(pointMinY, pointMaxY)
+            pyautogui.moveTo(pointX, pointY)
+            pyautogui.click()
+            print("点击X:", pointX, ";Y:", pointY)
+
+            # 停留随机秒数
+            sleepSec = random.randint(1, 3)
+            time.sleep(sleepSec)
+
+            pointX = random.randint(pointMinX, pointMaxX)
+            pointY = random.randint(pointMinY, pointMaxY)
+            pyautogui.moveTo(pointX, pointY)
+            pyautogui.click()
+            print("点击X:", pointX, ";Y:", pointY)
+            # 停留随机秒数
+            sleepSec = random.randint(1, 3)
+            time.sleep(sleepSec)
+
+            print("start getCurrentPageData")
+            pyautogui.moveTo(gl_refresh_button_x, gl_refresh_button_y, 1)
+            pyautogui.click()
+            time.sleep(1)
+
+            print("end getCurrentPageData")
+
+            pointX = random.randint(pointMinX, pointMaxX)
+            pointY = random.randint(pointMinY, pointMaxY)
+            pyautogui.moveTo(pointX, pointY, 0.5)
+            pyautogui.click()
+            print("点击X:", pointX, ";Y:", pointY)
+            # 停留等待数据更新
+            time.sleep(get_data_timeout_time)
+
+            pyautogui.hotkey('ctrl', 's')
+            time.sleep(1)
+            pyautogui.hotkey('ctrl', 'w')
+
+            return True
+            # wb.save()
+            #
+            # wb.close()
+        else:
+            print("ext err:" + fileFullPath)
+        # app.kill()
+        return True
+    except Exception as e:
+        print("Exception:")
+        print(str(e))
+        return False
+
+
+if __name__ == "__main__":
+    app = __hug__.http.server()
+    run(app=app, reloader=True, port=7007)

+ 195 - 0
rpa_smm_refresh.py

@@ -0,0 +1,195 @@
+import hug
+from bottle import route, run
+import pyautogui
+import random
+import time
+import os
+import pythoncom
+import win32gui, win32con, win32process
+import pyperclip
+# import psutil
+import urllib.parse
+import autoit
+import datetime
+# import subprocess
+
+hug.API(__name__).http.output_format = hug.output_format.json
+
+exePath = r"C:\Program Files (x86)\Kingsoft\WPS Office\11.8.2.12085\office6\et"
+# exePath = exePath = r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
+# exePath = exePath = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
+
+@hug.get('/smm_chemical/server')
+def smm_server():
+    return 1
+
+# 顶部有色按钮的x,y位置
+smm_button_x = 642
+smm_button_y = 53
+
+# 中间更新数据下拉框按钮的x,y位置
+smm_refresh_select_button_x = 212
+smm_refresh_select_button_y = 119
+
+# 中间更新数据按钮的x,y位置
+smm_refresh_button_x = 232
+smm_refresh_button_y = 178
+
+# 超时时间,默认15s
+timeout_time = 15
+
+
+# 随机点击的按钮范围
+pointMinX = 200
+pointMaxX = 650
+pointMinY = 300
+pointMaxY = 550
+
+@hug.get('/smm_chemical/refresh')
+def smm_refresh(file_path):
+    screen_width, screen_height = pyautogui.size()
+    print("当前时间:{};当前屏幕分辨率:{} x {}".format(datetime.datetime.now(), screen_width, screen_height))
+
+    pyautogui.FAILSAFE = False
+
+    file_path = file_path.replace('"', '')
+
+    # 显示桌面
+    pyautogui.hotkey('win', 'd')
+
+    try:
+        file_path = urllib.parse.unquote(file_path)
+        fileFullPath = file_path
+
+        # print("full_path start")
+        fileFullPath = fileFullPath.replace("\\\\", "\\")
+
+        print("文件路径:", fileFullPath)
+
+        if not os.path.exists(fileFullPath):
+            print("文件不存在")
+            return
+
+        if os.path.exists(fileFullPath) and ".xlsx" in fileFullPath \
+                and fileFullPath.find("~$") < 0 \
+                and fileFullPath.find("template") < 0:
+
+            # 获取文件名和扩展名
+            file_name, file_ext = os.path.splitext(os.path.basename(fileFullPath))
+            file_name_ext = file_name + file_ext
+            # print(file_name_ext)
+
+            try:
+
+                # 启动程序
+                code = exePath + " " + fileFullPath
+                # print(code)
+                autoit.run(code)
+                autoit.win_wait_active(file_name_ext, timeout=20)  # 等待窗口激活
+                autoit.win_move(file_name_ext, 0, 0, 1024, 768)
+                # autoit.win_move(file_name_ext, 0, 0, 1920, 1080)
+
+                # print("监听成功了")
+
+            except Exception as e:
+                print("监听", fileFullPath, "失败:", e)
+
+                ## 失败的话,截图记录下原因
+                screenshot = pyautogui.screenshot()
+                imgPath = datetime.datetime.now().strftime("%Y%m%d%H%M%S")+"_screenshot_img.png"
+                screenshot.save(imgPath)
+
+                hwnd = win32gui.GetForegroundWindow()
+                found_title = win32gui.GetWindowText(hwnd)
+                print("当前窗口名称:", found_title)
+                if found_title == "":
+                    print("窗口未打开")
+                    return
+                if found_title.startswith(file_name_ext) == False:
+                    print("当前窗口与excel不一致,当前窗口:", found_title, ";excel:", file_name_ext)
+                    return
+                autoit.win_move_by_handle(hwnd, 0, 0, 1024, 768)
+                # autoit.win_move_by_handle(hwnd, 0, 0, 1920, 1080)
+
+            # 等待自动刷新结束
+            #time.sleep(15)
+
+            # 保存
+            #pyautogui.hotkey('ctrl', 's')
+            #time.sleep(1)
+
+            # 关闭当前excel
+            #pyautogui.hotkey('ctrl', 'w')
+
+            #return True
+
+            # 屏幕最大化
+            hwnd = win32gui.GetForegroundWindow()
+            win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
+            time.sleep(1)
+
+            # 随机2-3次点击 单元格区域
+            r = random.randint(2, 3)
+            for i in range(r):
+                pointX = random.randint(pointMinX, pointMaxX)
+                pointY = random.randint(pointMinY, pointMaxY)
+                print("随机点击", i + 1, "下;", "点击X:", pointX, ";Y:", pointY)
+                pyautogui.moveTo(pointX, pointY, 0.5)
+                pyautogui.click()
+                time.sleep(1)
+
+            # 随机停留几秒
+            r = random.randint(1, 2)
+            time.sleep(r)
+
+            # 点击 SMM DATA PRO 按钮
+            # point_x = 541
+            # point_y = 63
+            pyautogui.moveTo(smm_button_x, smm_button_y, 0.5)
+            pyautogui.click()
+            time.sleep(1)
+
+            # 点击 刷新下拉框 按钮
+            # point_x = 210
+            # point_y = 142
+            pyautogui.moveTo(smm_refresh_select_button_x, smm_refresh_select_button_y, 0.5)
+            pyautogui.click()
+            time.sleep(1)
+
+            # 点击 全部工作表 按钮
+            print("开始刷新了")
+            # point_x = 240
+            # point_y = 233
+            pyautogui.moveTo(smm_refresh_button_x, smm_refresh_button_y, 0.5)
+            pyautogui.click()
+            time.sleep(0.5)
+
+            pointX = random.randint(pointMinX, pointMaxX)
+            pointY = random.randint(pointMinY, pointMaxY)
+            # 点击完刷新按钮后,移开鼠标
+            pyautogui.moveTo(pointX, pointY, 0.5)
+
+            # 等待刷新结束
+            time.sleep(timeout_time)
+
+            # 保存
+            pyautogui.hotkey('ctrl', 's')
+            time.sleep(1)
+
+            # 关闭当前excel
+            pyautogui.hotkey('ctrl', 'w')
+
+            return True
+        else:
+            print("ext err:" + fileFullPath)
+
+        return True
+    except Exception as e:
+        print("Exception:")
+        print(str(e))
+        return False
+
+
+if __name__ == "__main__":
+    app = __hug__.http.server()
+    run(app=app, reloader=True, port=7007)

+ 41 - 0
ths_api.py

@@ -61,6 +61,47 @@ def GetFutureGoodEdbDataByThs(EdbCode, StartDate, EndDate):
     print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetFutureGoodEdbDataByThs")
     return result
 
+def ths_data_to_dict(ths_instance):
+    if ths_instance is not None and ths_instance.data is not None:
+        return {
+            'errorcode': ths_instance.errorcode,
+            'errmsg': ths_instance.errmsg,
+            'data': ths_instance.data.to_dict(orient='records')
+        }
+    else:
+        # 处理 ths_instance 为 None 的情况
+        return {
+            'errorcode': 'N/A',
+            'errmsg': 'N/A',
+            'data': []
+        }
+
+def has_comma(input_string):
+    return isinstance(input_string, str) and ',' in input_string
+
+def process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate):
+    thsEDBDataQuery = THS.THS_DS(StockCode, SingleEdbCode, '', '', StartDate, EndDate)
+    print(thsEDBDataQuery)
+    result = json.dumps(ths_data_to_dict(thsEDBDataQuery))
+    return result
+
+@hug.get('/edbInfo/ths/ds')
+def GetEdbDataByThsDs(StockCode, EdbCode, StartDate, EndDate):
+    print("THS_DataSequence start")
+    print(EdbCode)
+    result_list = []
+
+    if isinstance(EdbCode, list):
+        for SingleEdbCode in EdbCode:
+            result_list.append(process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate))
+            print("THS_DS end")
+        print(result_list)
+        return result_list
+    else:
+        # 如果不包含逗号,直接处理
+        result = process_single_edb_code(StockCode, EdbCode, StartDate, EndDate)
+        print(result)
+        return result
 
 if __name__ == "__main__":
     # ths登录函数

+ 9 - 27
wind_api.py

@@ -49,9 +49,9 @@ def GetEdbDataByWind(EdbCode, StartDate, EndDate):
     return result
 
 
-@hug.get('/edbInfo/wind/future_good')
-def GetFutureGoodEdbDataByWind(FutureGoodEdbCode, StartDate, EndDate):
-    print("GetFutureGoodEdbDataByWind:", FutureGoodEdbCode)
+@hug.get('/edbInfo/wind/wsd')
+def GetEdbDataWindWsd(StockCode,EdbCode, StartDate, EndDate):
+    print("GetEdbDataByWind:", EdbCode)
     isConnected = w.isconnected()
     print("isconnected")
     print(isConnected)
@@ -62,38 +62,20 @@ def GetFutureGoodEdbDataByWind(FutureGoodEdbCode, StartDate, EndDate):
         if isConnected == False:
             return "{'ErrMsg':'启动Wind接口失败'}"
 
-    print("getdata")
     option = "Fill=Previous"
-    data = w.edb(FutureGoodEdbCode, StartDate, EndDate, option)
-    data = w.wsd(FutureGoodEdbCode, "trade_code,open,high,low,close,volume,amt,oi,settle", StartDate, EndDate, option)
-    print("wind data")
-    print(data)
-    df = pd.DataFrame()
+    wsd_data = w.wsd(StockCode,EdbCode, StartDate, EndDate, option)
 
-    if data.ErrorCode == -40521010: # Internet Timeout 超时退出
+    if wsd_data.ErrorCode == -40521010: # Internet Timeout 超时退出
         os._exit(0)
         return "a"
-
-    df['DT'] = data.Times
-    df['TRADE_CODE'] = data.Data[0]
-    df['OPEN'] = data.Data[1]
-    df['HIGH'] = data.Data[2]
-    df['LOW'] = data.Data[3]
-    df['CLOSE'] = data.Data[4]
-    df['VOLUME'] = data.Data[5]
-    df['AMT'] = data.Data[6]
-    df['OI'] = data.Data[7]
-    df['SETTLE'] = data.Data[8]
-    df['ErrorCode'] = data.ErrorCode
-    df = df[['DT', 'TRADE_CODE', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'VOLUME', 'AMT', 'OI', 'SETTLE', 'ErrorCode']]
-    df = df.dropna()
-    json_data = df.to_json()
-    # w.stop()
-    print("wind data end")
+   
+    fm=pd.DataFrame(wsd_data.Data,index=wsd_data.Fields,columns=wsd_data.Times)
+    json_data=fm.to_json()
     result = json.loads(json_data)
     return result
 
 
+
 if __name__ == "__main__":
     # wind 登录
     wStart = w.start()

File diff suppressed because it is too large
+ 17 - 0
yongyi_pig/imgcode_ak.py


+ 29 - 0
yongyi_pig/readme.txt

@@ -0,0 +1,29 @@
+python+selunium定位已打开的浏览器
+参考文档:https://blog.51cto.com/u_15800928/6952328?articleABtest=0
+我们可以利用Chrome DevTools协议。它允许客户检查和调试Chrome浏览器。
+
+1、找到谷歌浏览器的安装地址,并开启一个新的端口,设置一个文件夹保存浏览器的数据
+mac 脚本:
+>/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222  --user-data-dir=/Users/xiexiaoyuan/data/selenium/automationProfile
+
+windows 脚本:
+打开cmd,输入命令:
+>cd C:\Program Files\Google\Chrome\Application\
+>chrome.exe   --remote-debugging-port=9222  --user-data-dir=D:\data\chrome_selunium\automationProfile
+
+-remote-debugging-port值,可以指定任何打开的端口
+-user-data-dir标记,指定创建新Chrome配置文件的目录。它是为了确保在单独的配置文件中启动chrome,不会污染你的默认配置文件。
+
+此时会打开一个浏览器页面,我们输入目标网址,输入账号密码,登录成功。
+登录之后,以后都不需要登录,它会把你这次登录的信息记入到--user-data-dir指定的目录 下
+后面你只需要python+selenium+webdriver定位到这个已经登录的浏览器进行操作就可以啦
+2、运行python脚本: yongyi_pig_manual.py
+
+注意:1、不确定涌溢的网址登录成功后的有效期有多长,目前测试来看至少有1天时间,如果超过登录有效期,需要人工重新登录网页,
+     2、如果本地机器断电重启,即浏览器被关闭,也需要人工重新运行浏览器的脚本
+
+
+python+selunium+第三方服务商识别图片验证码并自动登录
+我测试了20来次,图片验证码的识别准确率有80%,服务费用:0/500次, 298元/3万次
+第三方服务商网址:https://market.aliyun.com/apimarket/detail/cmapi00035185?spm=5176.730005.result.6.3fcb3524emVIdg&accounttraceid=020a0ff01c6848a592d9f028b5356d2chklv
+运行脚本:yongyi_pig.py

+ 185 - 0
yongyi_pig/yongyi_pig.py

@@ -0,0 +1,185 @@
+# coding:utf-8
+from time import sleep
+import datetime
+import openpyxl
+import requests
+from selenium import webdriver
+
+# 设置Chrome浏览器选项
+from selenium.common import exceptions
+from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support import expected_conditions as EC
+from selenium.webdriver.support.wait import WebDriverWait
+
+from imgcode_ak import image_code
+
+"""
+根据table的id属性和table中的某一个元素定位其在table中的位置
+table包括表头,位置坐标都是从1开始算
+tableId:table的id属性
+queryContent:需要确定位置的内容
+"""
+
+
+def get_table_content(driver, tableId, queryContent):
+    # 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据
+    table_tr_list = driver.find_element(By.ID, tableId).find_elements(By.TAG_NAME, "tr")
+    table_list = []  # 存放table数据
+    for tr in table_tr_list:  # 遍历每一个tr
+        # 将每一个tr的数据根据td查询出来,返回结果为list对象
+        table_td_list = tr.find_elements(By.TAG_NAME, "td")
+        row_list = []
+        print(table_td_list)
+        for td in table_td_list:  # 遍历每一个td
+            row_list.append(td.text)  # 取出表格的数据,并放入行列表里
+        table_list.append(row_list)
+
+    # 循环遍历table数据,确定查询数据的位置
+    # for i in range(len(table_list)):
+    #     for j in range(len(table_list[i])):
+    #         if queryContent == table_list[i][j]:
+    #             print("%r坐标为(%r,%r)" % (queryContent, i + 1, j + 1))
+
+
+# 写入文件
+def write_excel_xlsx(path, sheet_name, value):
+    index = len(value)  # 列表中所含元组的个数,从而确定写入Excel的行数
+    # 打开Excel
+    wb = openpyxl.Workbook()
+    # wb = load_workbook(path)
+    sheet = wb.active  # 获得一个的工作表
+    sheet.title = sheet_name
+    # 设置格式
+    sheet.column_dimensions['B'].width = 115
+    # 按行加入
+    for i in range(index):
+        sheet.append(value[i])
+    # 保存文件
+    print(sheet.values)
+    wb.save(path)
+    print("题目写入数据成功!")
+
+
+def send_file(url, file_path):
+    with open(file_path, 'rb') as file:
+        files = {'file': file}
+        response2 = requests.post(url, files=files)
+    return response2
+
+
+def get_element(my_driver, xpaths):
+    """
+    判断是否存在元素并获取元素对象
+    :param my_driver:
+    :param xpaths: xpaths表达式
+    :return: 元素对象或为空
+    """
+    try:
+        target = my_driver.find_element(By.XPATH, xpaths)
+    except exceptions.NoSuchElementException:
+        return False
+    else:
+        return target
+
+
+if __name__ == "__main__":
+    # 创建一个 Chrome WebDriver 实例
+    options = webdriver.ChromeOptions()
+    # options.add_argument("headless")
+    # options.add_argument('--headless')
+    options.add_argument('--disable-gpu')
+    options.add_argument('--no-sandbox')
+    options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, '
+                         'like Gecko) Chrome/118.0.5993.70 Safari/537.36')
+    options.add_argument(" window-size=1920,1080")
+    prefs = {'download.prompt_for_download': False, 'download.default_directory': '/Users/xiexiaoyuan/Downloads/'}
+    options.add_experimental_option('prefs', prefs)
+    s = Service(executable_path='/Users/xiexiaoyuan/chromedriver_mac64_114/chromedriver')
+    # s = Service(executable_path='/Users/xi/Desktop/chromedriver')
+    driver = webdriver.Chrome(service=s, options=options)
+    # driver.maximize_window()
+    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
+        "source": """
+                            Object.defineProperty(navigator, 'webdriver', {
+                              get: () => undefined
+                            })
+                          """
+    })
+    driver.get('https://data.yongyizixun888.com/index.php?s=member&c=login&m=index')
+    sleep(2)
+
+    admin_name = ''
+    admin_pwd = ''
+    table = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[2]/img')
+    table.screenshot(r'pig.png')
+
+    sleep(2)
+    # 解析图片验证码
+    code = image_code('pig.png')
+    if code == "":
+        print("获取图片验证码失败")
+
+    print(code)
+    # 模拟登录
+    # 输入账号
+    username = driver.find_element(By.XPATH, '//*[@id="myform"]/div[1]/div/input')
+    username.send_keys(admin_name)
+    sleep(1)
+    # 输入密码
+    password = driver.find_element(By.XPATH, '//*[@id="myform"]/div[2]/div/input')
+    password.send_keys(admin_pwd)
+    sleep(1)
+    # 输入验证码
+    capter = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[1]/input')
+    capter.send_keys(code)
+
+    sleep(1)
+    # 登录
+    signup = driver.find_element(By.XPATH, '//*[@id="myform"]/div[4]/button')
+    signup.click()
+
+    sleep(5)
+    # 没有登录成功重试
+    account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text
+    print(account)
+    if account != admin_name:
+        # 解析图片验证码
+        table = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[2]/img')
+        table.screenshot(r'pig.png')
+        code = image_code('pig.png')
+        if code == "":
+            print("获取图片验证码失败")
+            # 输入验证码
+        capter = driver.find_element(By.XPATH, '//*[@id="myform"]/div[3]/div/div[1]/input')
+        capter.clear()
+        capter.send_keys(code)
+
+        sleep(1)
+        # 登录
+        signup = driver.find_element(By.XPATH, '//*[@id="myform"]/div[4]/button')
+        signup.click()
+
+    sleep(2)
+
+    account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text
+    print(account)
+    if account == admin_name:
+        # 跳转首页
+        driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[3]/ul/li[1]/a').click()
+        sleep(2)
+        a = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[2]/a')
+        print(a.get_attribute("href"))
+        a.click()
+
+        # 下载涌溢完整数据库
+        sleep(1)
+        b = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[3]/a')
+        print(b.get_attribute("href"))
+        b.click()
+        sleep(10)
+        print("下载成功")
+    else:
+        print("登录失败")
+
+    driver.close()

+ 152 - 0
yongyi_pig/yongyi_pig_manual.py

@@ -0,0 +1,152 @@
+# coding:utf-8
+import os
+import shutil
+from time import sleep
+from bottle import route, run
+import datetime
+
+import hug
+import rarfile as rarfile
+from selenium import webdriver
+
+# 设置Chrome浏览器选项
+from selenium.common import exceptions
+from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support import expected_conditions as EC
+from selenium.webdriver.support.wait import WebDriverWait
+
+
+def rename_week_file(new_dir, current_time, rar_name):
+    files = os.listdir(rar_name)
+    for dir in files:
+        file_path = os.path.join(rar_name, dir)
+        if os.path.isdir(file_path):
+            for f in os.listdir(file_path):
+                print("f.title()" + f.title())
+                if f.title().find("周度数据") != -1:
+                    new_name = f'{new_dir}/{current_time}_week.xlsx'
+                    old_name = os.path.join(file_path, f)
+                    # os.rename(old_name, new_name)
+                    shutil.copy(old_name, new_name)
+                    print(f'周度文件重命名成功, 旧文件名{old_name} 新文件名{new_name}')
+                    return
+        else:
+            print(dir.title() + "不是一个文件夹")
+            return
+    return
+
+@hug.get('/yongyi/download')
+def yongyi_download(dayFlag, weekFlag):
+    print("dayFlag:"+dayFlag)
+    print("weekFlag:"+weekFlag)
+
+    try:
+        # python+selunium定位已打开的浏览器
+        # 创建一个 Chrome WebDriver 实例
+        options = webdriver.ChromeOptions()
+        # options.add_argument("headless")
+        # options.add_argument('--headless')
+        options.add_argument('--disable-gpu')
+        options.add_argument('--no-sandbox')
+        # 谷歌浏览器运行的默认调试端口:先用以下命令启动浏览器
+        # 找到谷歌浏览器的程序地址,开启一个新的端口,并设置一个文件夹来保存浏览器的数据
+        # /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222  --user-data-dir=/Users/xiexiaoyuan/data/selenium/automationProfile
+        # --remote-debugging-port值,可以指定任何打开的端口
+        # --user-data-dir标记,指定创建新Chrome配置文件的目录。它是为了确保在单独的配置文件中启动chrome,不会污染你的默认配置文件。
+        #
+        # 此时会打开一个浏览器页面,我们输入目标网址,输入账号密码,登录成功。
+        # 登录之后,以后都不需要登录,它会把你这次登录的信息记入到 --user-data-dir指定的目录下
+        # 后面你只需要python + selenium + webdriver定位到这个已经登录的浏览器进行操作就可以啦
+        options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
+        # 修改下载地址
+        # save_to_dir = '/Users/xiexiaoyuan/Downloads'
+        save_to_dir = r'D:/eta/yongyi_data/download'
+        options.add_argument("--download.default_directory=" + save_to_dir)
+        options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, '
+                             'like Gecko) Chrome/118.0.5993.70 Safari/537.36')
+        options.add_argument(" window-size=1920,1080")
+
+        # s = Service(executable_path='/Users/xiexiaoyuan/chromedriver_mac64_114/chromedriver')
+        # s = Service(executable_path='D:/download/chromedriver119-win64/chromedriver.exe')
+        s = Service(executable_path='D:/eta/chromedriver-win64/chromedriver.exe')
+        driver = webdriver.Chrome(service=s, options=options)
+        driver.get('https://data.yongyizixun888.com/')
+        sleep(3)
+
+        account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text
+        print(account)
+
+        # 下载涌溢日度数据库
+        if dayFlag == '1':
+            sleep(1)
+            a = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[2]/a')
+            print(a.get_attribute("href"))
+            a.click()
+            sleep(30)
+
+        # 下载涌溢完整数据库
+        if weekFlag == '1':
+            sleep(2)
+            b = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[3]/a')
+            print(b.get_attribute("href"))
+            b.click()
+            sleep(30)
+
+        # 获取当前时间,并将其格式化为指定的形式
+        # new_dir = '/Users/xiexiaoyuan/Downloads/yongyi'
+        new_dir = r'D:/eta/yongyi_data/yongyi'
+        current_time = datetime.datetime.now().strftime("%Y-%m-%d")
+        current_time_full = datetime.datetime.now().strftime('%Y{y}%m{m}%d{d}').format(y='年', m='月', d='日')
+        print(current_time_full)
+
+        # 查找文件并重命名
+        os.chdir(save_to_dir)
+        files = filter(os.path.isfile, os.listdir(save_to_dir))
+        files = [os.path.join(save_to_dir, f) for f in files]  # add path to each file
+        files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
+        day_file = ""
+        week_file = ""
+        for file in files:
+            day_name = f'{current_time_full}涌益咨询日度数据'
+            if file.title().find(day_name) != -1:
+                if day_file == "":
+                    day_file = file
+            if file.title().find("涌益咨询周度数据") != -1:
+                if week_file == "":
+                    week_file = file
+            if day_file != "" and week_file != "":
+                break
+
+        if dayFlag == '1':
+            if day_file != "":
+                print(day_file.title())
+                new_name = f'{new_dir}/{current_time}_day.xlsx'
+                # os.rename(day_file.title(), new_name)
+                shutil.copy(day_file.title(), new_name)
+                print(f'日度文件重命名成功, 旧文件名{day_file.title()} 新文件名{new_name}')
+            else:
+                print("未找到日度下载文件")
+
+        if weekFlag == '1':
+            if week_file != "":
+                print(week_file.title())
+                filename = week_file.title()
+                index = filename.find(".Rar")
+                rar_name = filename[:index]
+                # 解压缩
+                rar_file = rarfile.RarFile(filename, 'r')
+                rar_file.extractall(rar_name)
+                rar_file.close()
+                rename_week_file(new_dir, current_time, rar_name)
+            else:
+                print("未找到周度下载文件")
+        return True
+    except Exception as e:
+        print("Exception:")
+        print(str(e))
+        return False
+
+if __name__ == "__main__":
+    app = __hug__.http.server()
+    run(app=app, reloader=True, host='127.0.0.1', port=7010)

Some files were not shown because too many files changed in this diff