当前位置: 首页 > news >正文

Python 自动化 打开网站 填表登陆 例子

图样

简价:

简要说明这个程序的功能:

1. **基本功能**:
   - 自动打开网站
   - 自动填写登录信息(号、公司名称、密码)
   - 显示半透明状态窗口实时提示操作进度

2. **操作流程**:
   - 打开网站后自动点击两个位置(1515,130)和(990,355)
   - 依次填写:税号 → 公司名称 → 密码
   - 最后自动按回车确认

3. **配置参数**:
```python
CONFIG = {
    'tax_id': 'xxxxxxx',    # 号
    'company_name': '登记名字',       # 公司名称
    'password': 'abc',              # 密码
    'wait_time': 1,                 # 每步操作等待时间
    'initial_delay': 3,             # 初始等待时间
    'url': 'https://xxxxxx/'  # 网站地址
}
```

只需要修改 CONFIG 中的参数就可以适应不同的登录信息。
 

代码: 

# -*- coding: utf-8 -*-
import pyautogui
import time
import pyperclip
import logging
import webbrowser
import tkinter as tk
from typing import Optional# 配置日志
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s'
)# 配置参数
CONFIG = {'tax_id': 'xxxxxxx号','company_name': '登记名字','password': 'abc','wait_time': 1,'initial_delay': 3,'url': 'https://xxxxxxxx/'
}class StatusWindow:def __init__(self):self.root = tk.Tk()self.root.title("自动操作状态")# 设置窗口大小和位置self.root.geometry("300x100+100+100")# 设置窗口透明度self.root.attributes('-alpha', 0.8)# 设置窗口置顶self.root.attributes('-topmost', True)# 创建标签self.label = tk.Label(self.root,text="准备开始...",font=('微软雅黑', 12),wraplength=280)self.label.pack(pady=20)def update_status(self, message: str):"""更新状态信息"""self.label.config(text=message)self.root.update()def close(self):"""关闭窗口"""self.root.destroy()def open_tax_website() -> None:"""打开网站"""try:logging.info("正在打开网站...")webbrowser.open(CONFIG['url'])# 等待浏览器加载time.sleep(CONFIG['initial_delay'])# 移动到第一个位置并点击logging.info("移动到第一个位置...")pyautogui.moveTo(1515, 130)pyautogui.click()# 等待5秒time.sleep(5)# 移动到第二个位置并点击logging.info("移动到第二个位置...")pyautogui.moveTo(990, 355)pyautogui.click()# 等待1秒time.sleep(1)except Exception as e:logging.error(f"打开网站或移动鼠标时发生错误: {str(e)}")raisedef safe_paste(text: str, wait_time: Optional[float] = None) -> None:"""安全地执行复制粘贴操作"""wait_time = wait_time or CONFIG['wait_time']try:pyperclip.copy(text)time.sleep(wait_time)pyautogui.hotkey('ctrl', 'v')time.sleep(wait_time)except Exception as e:logging.error(f"粘贴文本 '{text}' 时发生错误: {str(e)}")raisedef safe_key_press(key: str, wait_time: Optional[float] = None) -> None:"""安全地执行按键操作"""wait_time = wait_time or CONFIG['wait_time']try:pyautogui.press(key)time.sleep(wait_time)except Exception as e:logging.error(f"按键 '{key}' 操作发生错误: {str(e)}")raisedef fill_tax_form() -> None:"""填写表单的主要函数"""status_window = StatusWindow()try:status_window.update_status("开始填写表单...")logging.info("开始填写表单...")# 首先打开网站status_window.update_status("正在打开税务网站...")open_tax_website()status_window.update_status("正在进行页面操作...")# 等待页面加载完成status_window.update_status("等待页面加载...")time.sleep(CONFIG['initial_delay'])# 输入税号status_window.update_status("正在输入税号...")logging.info("正在输入号...")safe_paste(CONFIG['tax_id'])safe_key_press('tab')# 输入公司名称status_window.update_status("正在输入公司名称...")logging.info("正在输入公司名称...")safe_paste(CONFIG['company_name'])safe_key_press('tab')# 输入密码status_window.update_status("正在输入密码...")logging.info("正在输入密码...")safe_paste(CONFIG['password'])safe_key_press('enter')  # 直接按回车确认status_window.update_status("表单填写完成!")logging.info("表单填写完成")time.sleep(2)  # 显示完成信息2秒except Exception as e:status_window.update_status(f"发生错误: {str(e)}")logging.error(f"填写表单时发生错误: {str(e)}")time.sleep(3)  # 显示错误信息3秒raisefinally:status_window.close()if __name__ == "__main__":try:fill_tax_form()print('表单填写已完成')except Exception as e:print(f'程序执行出错: {str(e)}')

http://www.lryc.cn/news/509300.html

相关文章:

  • 【Chrome】浏览器提示警告Chrome is moving towards a new experience
  • 网络下载ts流媒体
  • iDP3复现代码模型训练全流程(一)——train_policy.sh
  • 重温设计模式--单例模式
  • 【人工智能】Python中的机器学习管道:如何用scikit-learn构建高效的ML管道
  • Redis存在安全漏洞
  • Scala图书管理系统
  • 【数据可视化案列】白葡萄酒质量数据的EDA可视化分析
  • Postman接口测试:全局变量/接口关联/加密/解密
  • vue+elementui实现下拉表格多选+搜索+分页+回显+全选2.0
  • 电商系统-产品经理
  • 《庐山派从入门到...》PWM板载蜂鸣器
  • 【河南新标】豫财预〔2024〕105号-《关于省级政务信息化建设项目支出预算标准的规定》-费用标准解读系列29
  • 【数据结构】数据结构整体大纲
  • 【C++基础】09、结构体
  • 大恒相机开发(2)—Python软触发调用采集图像
  • QT的前景与互联网岗位发展
  • 青藤入选工信部“2024年网络安全技术应用典型案例拟支持项目名单”
  • NVIDIA GPU 内部架构介绍
  • 重温设计模式----装饰模式
  • 第十六章 C++ 字符串
  • MySQL中Seconds_Behind_Master是怎么计算的
  • React Native 集成原生Android功能
  • Mac mini m4安装PD和Crack和关闭SIP
  • 详解C语言中的关键词:extern以及它需要注意的事项
  • 框架程序设计-简答以及论述
  • ce第六次作业
  • 为何页面搜索应避免左模糊和全模糊查询???
  • AI可信论坛亮点:合合信息分享视觉内容安全技术前沿
  • 在 Mac M2 上安装 PyTorch 并启用 MPS 加速的详细教程与性能对比