如何使用 Python 自动执行日常任务(第 2 部分)
从现在开始,努力学习吧!本文《如何使用 Python 自动执行日常任务(第 2 部分)》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

作者:特里克斯·赛勒斯
waymap渗透测试工具:点击这里
trixsec github:点击这里
在第 1 部分中,我们探索了如何使用 python 来自动化文件管理、网页抓取、发送电子邮件、google 表格和系统监控。在第 2 部分中,我们将继续介绍更高级的任务,例如自动化 api、调度脚本以及将自动化与第三方服务集成。
7。自动化 api 请求
许多 web 服务提供 api 来以编程方式与其平台进行交互。使用请求库,您可以轻松地自动执行任务,例如从 api 获取数据、发布更新或在云服务上执行 crud 操作。
import requests
# openweathermap api configuration
api_key = 'your_api_key'
city = 'new york'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'
# send a get request to fetch weather data
response = requests.get(url)
data = response.json()
# extract temperature information
temperature = data['main']['temp']
weather = data['weather'][0]['description']
print(f"temperature: {temperature}°k")
print(f"weather: {weather}")
此脚本从 openweathermap api 获取指定城市的当前天气数据并显示它。
8。使用 python 安排任务
有时您需要自动执行任务以在特定时间或间隔运行。 python 的计划库可以轻松设置在特定时间自动运行的作业。
import schedule
import time
# task function to be executed
def task():
print("executing scheduled task...")
# schedule the task to run every day at 9 am
schedule.every().day.at("09:00").do(task)
# keep the script running to check the schedule
while true:
schedule.run_pending()
time.sleep(1)
此脚本安排任务在每天上午 9 点运行,使用简单的调度循环来保持任务运行。
9。自动化数据库操作
python 可用于与数据库交互、自动输入数据以及执行读取、更新和删除记录等操作。 sqlite3 模块允许您管理 sqlite 数据库,而其他库(如 psycopg2 或 mysqldb)可与 postgresql 和 mysql 配合使用。
import sqlite3
# connect to sqlite database
conn = sqlite3.connect('tasks.db')
# create a cursor object to execute sql commands
cur = conn.cursor()
# create a table for storing tasks
cur.execute('''create table if not exists tasks (id integer primary key, task_name text, status text)''')
# insert a new task
cur.execute("insert into tasks (task_name, status) values ('complete automation script', 'pending')")
# commit changes and close the connection
conn.commit()
conn.close()
此脚本创建一个 sqlite 数据库,添加一个“任务”表,并向数据库中插入一个新任务。
10。自动化 excel 文件管理
python 与 openpyxl 或 pandas 库一起可用于自动读取、写入和修改 excel 文件。这对于自动化数据分析和报告任务特别有用。
import pandas as pd
# read excel file
df = pd.read_excel('data.xlsx')
# perform some operation on the data
df['total'] = df['price'] * df['quantity']
# write the modified data back to a new excel file
df.to_excel('updated_data.xlsx', index=false)
此脚本读取 excel 文件,对数据执行计算,并将更新的数据写入新文件。
11。使用 selenium 实现浏览器交互自动化
使用 selenium,python 可以自动执行与 web 浏览器的交互,例如登录帐户、填写表单和执行重复的 web 任务。
from selenium import webdriver
from selenium.webdriver.common.keys import keys
# set up the browser driver
driver = webdriver.chrome()
# open the login page
driver.get('https://example.com/login')
# locate the username and password fields, fill them in, and log in
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
username.send_keys('your_username')
password.send_keys('your_password')
password.send_keys(keys.return)
# close the browser
driver.quit()
此脚本打开 web 浏览器,导航到登录页面,填写凭据,然后自动登录。
12。自动化云服务
python 与 aws、google cloud 和 azure 等云服务集成良好。使用 boto3 库,您可以自动执行管理 aws 中的 s3 存储桶、ec2 实例和 lambda 函数等任务。
import boto3
# connect to s3
s3 = boto3.client('s3')
# list all buckets
buckets = s3.list_buckets()
for bucket in buckets['buckets']:
print(bucket['name'])
# create a new bucket
s3.create_bucket(bucket='my-new-bucket')
# upload a file to the bucket
s3.upload_file('file.txt', 'my-new-bucket', 'file.txt')
此脚本连接到 aws s3,列出所有存储桶,创建一个新存储桶,并向其中上传文件。
13。自动化 pdf 操作
使用 pypdf2 库,python 可以自动执行合并、拆分和从 pdf 文件中提取文本等任务。
import PyPDF2
# List of PDF files to merge
pdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf']
# Create a PDF merger object
merger = PyPDF2.PdfMerger()
# Loop through the PDFs and append them to the merger
for pdf in pdfs:
merger.append(pdf)
# Write the merged PDF to a new file
with open('merged_file.pdf', 'wb') as f:
merger.write(f)
此脚本将多个 pdf 文件合并为一个文件。
~trixsec
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何使用 Python 自动执行日常任务(第 2 部分)》文章吧,也可关注golang学习网公众号了解相关技术文章。
如何在 Linux 下重新编译 Python 3?
- 上一篇
- 如何在 Linux 下重新编译 Python 3?
- 下一篇
- **网页滚动到特定位置触发事件:JavaScript/jQuery 如何实现?**
-
- 文章 · python教程 | 4小时前 |
- Python contextvars 为什么能隔离并发请求上下文
- 202浏览 收藏
-
- 文章 · python教程 | 8小时前 | Python教程 · 静态类型检查 异步方法 Python Protocol 结构类型 Awaitable
- Python Protocol 怎样描述带异步方法的结构类型
- 363浏览 收藏
-
- 文章 · python教程 | 11小时前 | Python教程 · InitVar __post_init__ Python dataclasses.replace 数据类复制
- Python dataclasses.replace 遇到 InitVar 时怎样传递参数
- 381浏览 收藏
-
- 文章 · python教程 | 13小时前 | python · pathlib ·
- Python importlib.resources.as_file 的临时路径何时失效
- 318浏览 收藏
-
- 文章 · python教程 | 15小时前 | SQLite · Python教程 · Python sqlite3 Connection.backup 进度回调 SQLite备份
- Python sqlite3 备份进度回调怎样判断剩余页数
- 264浏览 收藏
-
- 文章 · python教程 | 17小时前 | python · 内存优化 · Python教程 · 文件读取 大文件处理 Python mmap 分段映射 ALLOCATIONGRANULARITY
- Python mmap 怎样分段处理超过内存的大文件
- 146浏览 收藏
-
- 文章 · python教程 | 19小时前 | python · Python 二进制协议 零拷贝 memoryview
- Python memoryview 如何零拷贝切片二进制协议数据
- 225浏览 收藏
-
- 文章 · python教程 | 21小时前 | 并发控制 · Python教程 · asyncio · 虚假唤醒 wait_for Python asyncio asyncio.Condition 异步同步
- Python asyncio.Condition.wait_for 如何处理虚假唤醒
- 478浏览 收藏
-
- 文章 · python教程 | 23小时前 |
- Python ExceptionGroup 派生新组时如何保留异常元数据
- 417浏览 收藏
-
- 文章 · python教程 | 1天前 | 异常处理 · 并发编程 · Python教程 · asyncio · asyncio 结构化并发 ExceptionGroup except* Python TaskGroup
- Python TaskGroup 如何汇总多个子任务异常
- 208浏览 收藏
-
- 文章 · python教程 | 1天前 | 并发编程 · 工程实践 · Python教程 · 多进程日志 QueueListener multiprocessing.Queue RotatingFileHandler Python QueueHandler
- Python 日志 QueueHandler 解决多进程写入争用
- 186浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 395次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 476次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 481次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 426次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 251次使用
-
- Python sqlite3 Connection serialize 怎么导出数据库快照:备份窗口、内存占用与恢复校验
- 2026-08-26 501浏览
-
- Python监控网页状态:requests异常处理实战
- 2026-05-29 501浏览
-
- TensorFlow模型部署为API的TF Serving方法
- 2026-05-26 501浏览
-
- Python字符串编码转换:encode与decode详解
- 2026-05-16 501浏览
-
- TensorFlow裁剪无用算子方法详解
- 2026-05-15 501浏览
