主页
  • 主页
  • 分类
  • 热文
  • 教程
  • 面试
  • 标签
Python

Python 基础

Python 主页
Python 概述
Python 历史
Python 功能
Python 与 C++
Python Hello World
Python 应用领域
Python 解释器及其模式
Python 环境设置
Python 虚拟环境
Python 语法
Python 变量
Python 数据类型
Python 类型转换
Python Unicode 系统
Python 文字
Python 运算符
Python 算术运算符
Python 比较运算符
Python 赋值运算符
Python 逻辑运算符
Python 位运算符
Python 成员运算符
Python 身份运算符
Python 运算符优先级
Python 注释
Python 用户输入
Python 数字
Python 布尔值

Python 控制语句

Python 控制流
Python 决策
Python if 语句
Python if-else 语句
Python 嵌套 if 语句
Python match-case 语句
Python 循环
Python For 循环
Python for-else 循环
Python while 循环
Python break 语句
Python Continue 语句
Python pass 语句
Python 嵌套循环

Python 函数和模块

Python 函数
Python 默认参数
Python 关键字参数
Python 关键字专用参数
Python 位置参数
Python 仅限位置参数
Python 任意或可变长度参数
Python 变量作用域
Python 函数注释
Python 模块
Python 内置函数

Python 字符串

Python 字符串
Python 字符串切片
Python 字符串修改
Python 字符串连接
Python 字符串格式化
Python 转义字符
Python 字符串方法
Python 字符串练习

Python 列表

Python 列表
Python 访问列表项
Python 更改列表项
Python 添加列表项
Python 删除列表项
Python 循环列表
Python 列表推导式
Python 排序列表
Python 复制列表
Python 合并列表
Python 列表方法
Python 列表练习

Python 元组

Python 元组
Python 访问元组项
Python 更新元组
Python 解包元组项
Python 循环元组
Python 合并元组
Python 元组方法
Python 元组练习

Python 集合

Python 集合
Python 访问集合项
Python 添加集合项
Python 删除集合项
Python 循环集合
Python 合并集合
Python 复制集合
Python 集合运算符
Python 集合方法
Python 集合练习

Python 字典

Python 字典
Python 访问字典项
Python 更改字典项
Python 添加字典项
Python 移除字典项
Python 字典视图对象
Python 循环字典
Python 复制字典
Python 嵌套字典
Python 字典方法
Python 字典练习

Python 数组

Python 数组
Python 访问数组项
Python 添加数组项
Python 移除数组项
Python 循环数组
Python 复制数组
Python 反转数组
Python 排序数组
Python 合并数组
Python 数组方法
Python 数组练习

Python 文件处理

Python 文件处理
Python 文件写入
Python 文件读取
Python 重命名和删除文件
Python 目录
Python 文件方法
Python 文件/目录方法
Python OS.Path 方法

Python 面向对象编程

Python OOP 概念
Python 类和对象
Python 类属性
Python 类方法
Python 静态方法
Python 构造函数
Python 访问修饰符
Python 继承
Python 多态
Python 方法重写
Python 方法重载
Python 动态绑定
Python 动态类型
Python 抽象
Python 封装
Python 接口
Python 包
Python 内部类
Python 匿名类和对象
Python 单例类
Python 包装器类
Python 枚举
Python 反射

Python 错误和异常

Python 语法错误
Python 异常处理
Python Try-Except
Python Try-Finally
Python 抛出异常
Python 异常链
Python 嵌套 try
Python 用户定义异常
Python 日志记录
Python 断言
Python 内置异常

Python 多线程

Python 多线程
Python 线程生命周期
Python 创建线程
Python 启动线程
Python 合并线程
Python 命名线程
Python 线程调度
Python 线程池
Python 主线程
Python 线程优先级
Python 守护线程
Python 线程同步

Python 同步

Python 线程间通信
Python 死锁
Python 中断线程

Python 网络

Python 网络编程
Python 套接字编程
Python URL 处理
Python 泛型

Python 杂项

Python Date and Time
Python math模块
Python 迭代器
Python 生成器
Python 闭包
Python 装饰器
Python 递归
Python 正则表达式
Python Pip
Python 数据库访问
Python 弱引用
Python 序列化
Python 模板技术
Python 输出格式化
Python 性能测量
Python 数据压缩
Python 通用网关接口
Python XML 处理
Python 用户界面(GUI)
Python 命令行参数
Python Docstrings
Python JSON
Python 发送电子邮件
Python 进一步扩展
Python 工具/实用程序
Python GUI

Python 高级概念

Python 抽象基类
Python 自定义异常
Python 高阶函数
Python 对象的内部机制
Python 内存管理
Python 元类
Python 元编程
Python 模拟与桩
Python 猴子补丁
Python 信号处理
Python 类型提示
Python 进行自动化
Python Humanize包
Python 上下文管理器
Python 协程
Python 描述符
Python 内存泄漏
Python 不可变数据结构

基础

Python 主页
Python 概述
Python 历史
Python 功能
Python 与 C++
Python Hello World
Python 应用领域
Python 解释器及其模式
Python 环境设置
Python 虚拟环境
Python 语法
Python 变量
Python 数据类型
Python 类型转换
Python Unicode 系统
Python 文字
Python 运算符
Python 算术运算符
Python 比较运算符
Python 赋值运算符
Python 逻辑运算符
Python 位运算符
Python 成员运算符
Python 身份运算符
Python 运算符优先级
Python 注释
Python 用户输入
Python 数字
Python 布尔值

控制语句

Python 控制流
Python 决策
Python if 语句
Python if-else 语句
Python 嵌套 if 语句
Python match-case 语句
Python 循环
Python For 循环
Python for-else 循环
Python while 循环
Python break 语句
Python Continue 语句
Python pass 语句
Python 嵌套循环

函数和模块

Python 函数
Python 默认参数
Python 关键字参数
Python 关键字专用参数
Python 位置参数
Python 仅限位置参数
Python 任意或可变长度参数
Python 变量作用域
Python 函数注释
Python 模块
Python 内置函数

字符串

Python 字符串
Python 字符串切片
Python 字符串修改
Python 字符串连接
Python 字符串格式化
Python 转义字符
Python 字符串方法
Python 字符串练习

列表

Python 列表
Python 访问列表项
Python 更改列表项
Python 添加列表项
Python 删除列表项
Python 循环列表
Python 列表推导式
Python 排序列表
Python 复制列表
Python 合并列表
Python 列表方法
Python 列表练习

元组

Python 元组
Python 访问元组项
Python 更新元组
Python 解包元组项
Python 循环元组
Python 合并元组
Python 元组方法
Python 元组练习

集合

Python 集合
Python 访问集合项
Python 添加集合项
Python 删除集合项
Python 循环集合
Python 合并集合
Python 复制集合
Python 集合运算符
Python 集合方法
Python 集合练习

字典

Python 字典
Python 访问字典项
Python 更改字典项
Python 添加字典项
Python 移除字典项
Python 字典视图对象
Python 循环字典
Python 复制字典
Python 嵌套字典
Python 字典方法
Python 字典练习

数组

Python 数组
Python 访问数组项
Python 添加数组项
Python 移除数组项
Python 循环数组
Python 复制数组
Python 反转数组
Python 排序数组
Python 合并数组
Python 数组方法
Python 数组练习

文件处理

Python 文件处理
Python 文件写入
Python 文件读取
Python 重命名和删除文件
Python 目录
Python 文件方法
Python 文件/目录方法
Python OS.Path 方法

面向对象编程

Python OOP 概念
Python 类和对象
Python 类属性
Python 类方法
Python 静态方法
Python 构造函数
Python 访问修饰符
Python 继承
Python 多态
Python 方法重写
Python 方法重载
Python 动态绑定
Python 动态类型
Python 抽象
Python 封装
Python 接口
Python 包
Python 内部类
Python 匿名类和对象
Python 单例类
Python 包装器类
Python 枚举
Python 反射

错误和异常

Python 语法错误
Python 异常处理
Python Try-Except
Python Try-Finally
Python 抛出异常
Python 异常链
Python 嵌套 try
Python 用户定义异常
Python 日志记录
Python 断言
Python 内置异常

多线程

Python 多线程
Python 线程生命周期
Python 创建线程
Python 启动线程
Python 合并线程
Python 命名线程
Python 线程调度
Python 线程池
Python 主线程
Python 线程优先级
Python 守护线程
Python 线程同步

同步

Python 线程间通信
Python 死锁
Python 中断线程

网络

Python 网络编程
Python 套接字编程
Python URL 处理
Python 泛型

杂项

Python Date and Time
Python math模块
Python 迭代器
Python 生成器
Python 闭包
Python 装饰器
Python 递归
Python 正则表达式
Python Pip
Python 数据库访问
Python 弱引用
Python 序列化
Python 模板技术
Python 输出格式化
Python 性能测量
Python 数据压缩
Python 通用网关接口
Python XML 处理
Python 用户界面(GUI)
Python 命令行参数
Python Docstrings
Python JSON
Python 发送电子邮件
Python 进一步扩展
Python 工具/实用程序
Python GUI

高级概念

Python 抽象基类
Python 自定义异常
Python 高阶函数
Python 对象的内部机制
Python 内存管理
Python 元类
Python 元编程
Python 模拟与桩
Python 猴子补丁
Python 信号处理
Python 类型提示
Python 进行自动化
Python Humanize包
Python 上下文管理器
Python 协程
Python 描述符
Python 内存泄漏
Python 不可变数据结构

Python 进行自动化


上一章 下一章

利用 Python 进行自动化涉及使用编程技术自动执行任务,通常不需要人为干预。Python 提供了多种库,使其成为自动化不同类型重复任务的强大工具,包括任务调度、网络爬虫、GUI 自动化等等。

利用 Python 的广泛库,可以创建符合特定需求的自动化解决方案。

Python 自动化库

以下是一些常用的 Python 库,用于实现自动化:

  • unittest − Python 中的单元测试框架。
  • Selenium − 用于跨不同浏览器测试 Web 应用程序的 Web 自动化框架。
  • Beautiful Soup − 用于解析 HTML 和 XML 文档的库,适用于网络爬虫。
  • pandas − 数据处理库,适用于自动化数据分析任务。
  • requests − 用于发送 HTTP 请求和处理响应的 HTTP 库。
  • Selenium − 用于跨不同浏览器测试 Web 应用程序的 Web 自动化框架。
  • PyAutoGUI − 用于模拟鼠标和键盘动作的 GUI 自动化库。

在本教程中,您将了解到使用 Python 进行自动化的各个方面,从使用 schedule 模块安排任务,到使用 BeautifulSoup 进行网页抓取,再到使用 PyAutoGUI 进行 GUI 自动化。

这里我们提供了实际的例子,请确保在本地系统中使用以下命令安装必要的模块:

pip install schedule
pip install beautifulsoup4
pip install pyautogui

使用 “schedule” 模块进行 Python 自动化

Python 中的 schedule 模块允许在指定的时间间隔或时间安排任务以自动执行。它非常适合用于自动化诸如工作调度、定期任务或基于时间的事件之类的重复任务。

示例

此示例演示了如何使用 Python 的 schedule 模块安排每周会议、设置闹钟等常规任务。

import schedule
import time

# 定义计划任务的函数
def alarm():
   print("服务器重启时间到   ")

def job():
   print("开始每日任务   ")

def meet():
   print('周立会   ')

# 安排任务
schedule.every(5).seconds.do(alarm)  # 每隔 5 秒运行一次
schedule.every().day.at("10:30").do(job)  # 每天上午 10:30 运行
schedule.every().monday.at("12:15").do(meet)  # 每周一中午 12:15 运行

# 主循环执行计划的任务
while True:
   schedule.run_pending()
   time.sleep(0.5)

输出

执行上述代码后,您将看到类似如下输出:

服务器重启时间到   
服务器重启时间到   
服务器重启时间到 
...

Python 网络爬虫

网络爬虫是一个简单的自动化任务示例。它是一个自动从网站中提取信息的过程。该技术包括获取网页并从 HTML 内容中提取所需的数据。

网络爬虫广泛用于数据挖掘、数据分析以及需要从网络收集信息的自动化任务。

示例

此示例演示了如何使用 BeautifulSoup 和 urllib.request 模块抓取包含关键词 "python" 的网址的网页。

from bs4 import BeautifulSoup
from urllib.request import urlopen
import re

html = urlopen("https://www.tutorialspoint.com/python/index.htm")
content = html.read()
soup = BeautifulSoup(content, "html.parser")
for a in soup.findAll('a', href=True):
   if re.findall('python', a['href']):
      print("Python URL:", a['href'])

输出

执行上述代码后,您将看到类似如下输出:

Python URL: /machine_learning_with_python/index.htm
Python URL: /python_pandas/index.htm
Python URL: /python/index.htm
Python URL: /python/index.htm
Python URL: /python_pandas/index.htm
Python URL: /python_pillow/index.htm
Python URL: /machine_learning_with_python/index.htm
Python URL: /python_technologies_tutorials.htm
Python URL: /python/index.htm
...

使用 “pyautogui” 自动化鼠标移动

pyautogui 模块允许您的 Python 脚本自动执行鼠标移动、键盘输入和窗口交互,适用于测试 GUI 应用程序或自动化桌面上的重复任务。

示例

此示例演示了如何使用 Python 的 pyautogui 模块以方形模式自动移动鼠标。

import pyautogui

# 在较大的方形模式下移动鼠标
for i in range(5):
   pyautogui.moveTo(300, 100, duration=0.25)   
   pyautogui.move(300, 0, duration=0.25)       
   pyautogui.move(0, 300, duration=0.25)       
   pyautogui.move(-300, 0, duration=0.25)      
   pyautogui.move(0, -300, duration=0.25)      

输出

执行上述代码后,您将看到鼠标按照方形模式移动。

在 Python 中自动化单元测试

Python 中的 unittest 模块用于自动化测试。自动化单元测试是指为代码的各个单元(如函数或方法)编写测试,并自动运行这些测试以确保其行为符合预期。

Python 中的 unittest 模块是一个内置库,提供了创建和运行自动化测试的框架。

示例

此示例演示了如何使用 Python 的 unittest 模块对字符串方法执行自动化测试。每个测试用例方法都以 test_ 前缀开头,这是 unittest 框架识别为要运行的方法。

# 导入 unittest 模块
import unittest

class TestingStringMethods(unittest.TestCase):
   # 字符串相等测试
   def test_string_equality(self):
      # 如果两个参数相等则成功
      self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
   
   # 字符串比较测试
   def test_string_case(self):
      # 如果两个参数相等则成功
      self.assertEqual('Tutorialspoint'.upper(), 'TUTORIALSPOINT')

   # 检查字符串是否全大写
   def test_is_string_upper(self):
      # 用于检查语句是否为真或假
      self.assertTrue('TUTORIALSPOINT'.isupper())
      self.assertFalse('TUTORIALSpoint'.isupper())
      
   def test_string_startswith(self):
       # 使用 startswith() 来标识字符串的开头
       self.assertTrue('tutorialspoint'.startswith('putor'))
       self.assertFalse('tutorialspoint'.startswith('point'))
       
# 运行测试
unittest.main(verbosity=2)

输出

执行上述代码后,您将看到类似如下输出:

test_is_string_upper (__main__.TestingStringMethods) ... ok
test_string_case (__main__.TestingStringMethods) ... ok
test_string_equality (__main__.TestingStringMethods) ... ok
test_string_startswith (__main__.TestingStringMethods) ... FAIL

======================================================================
FAIL: test_string_startswith (__main__.TestingStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "main.py", line 23, in test_string_startswith
    self.assertTrue('tutorialspoint'.startswith('putor'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 4 tests in 0.000s

FAILED (failures=1)
上一章 下一章
阅读号二维码

关注阅读号

联系二维码

联系我们

© 2024 Yoagoa. All rights reserved.

粤ICP备18007391号

站点地图