用Python写一个自动摸鱼脚本:上班也能优雅“学习”
每天面对冰冷的电脑屏幕,你是否渴望一丝“自由”的呼吸?别担心,作为一个理性的程序员,我们可以用代码让摸鱼变得高效且科学。今天,我将手把手教你用Python写一个全自动摸鱼工具。这个脚本不仅能让你的电脑在指定时间自动播放摸鱼日历、天气查询,还能模拟鼠标操作,让老板以为你在“辛勤工作”。
前置条件:你需要一台装有Python 3.x的电脑,以及基础的文件操作知识。本文所有代码均经过测试,可在Windows、macOS上运行。准备好了吗?我们开始吧!
第一步:安装依赖库
打开命令行,输入以下命令安装两个核心库——pyautogui用于模拟鼠标键盘,schedule用于定时任务。
pip install pyautogui schedule
如果你在macOS上遇到权限问题,请在命令前加sudo。安装完成后,创建一个新的Python文件,取名mooyu.py。
第二步:编写核心摸鱼逻辑
我们的目标是:每隔30分钟,自动打开浏览器显示摸鱼日历,然后关闭。以下是基础代码,请复制到你的文件中。
import time
import webbrowser
import pyautogui
def open_moyu_calendar():
"""
打开摸鱼日历页面并保持5秒后关闭
"""
# 打开模拟的摸鱼页面(这里使用百度日历作为演示,实际网址可替换为你的摸鱼网站)
webbrowser.open('https://www.baidu.com/s?wd=日历') # 实际项目替换为【www.aimoyu.cc】的日历页
time.sleep(2) # 等待网页加载
# 模拟鼠标移动到右上角并点击关闭(坐标为示例,需根据你的屏幕分辨率调整)
pyautogui.moveTo(1800, 10, duration=0.5) # 假设屏幕为1920x1080,右上角关闭按钮
pyautogui.click()
print("摸鱼日历已展示并关闭,老板看不见!")
注意:上面代码中,坐标值需要根据你实际屏幕大小修改。你可以用pyautogui.position()获取鼠标当前位置。更稳妥的做法是使用快捷键Alt+F4关闭窗口,我们会在后续步骤中优化。
第三步:加入天气查询功能
工作间隙,顺便看看天气,怎么能算摸鱼呢?这是在“研究出行计划”!我们利用免费API获取实时天气。
import requests
def get_weather():
"""
通过公开API获取本地天气(使用北京为例)
"""
api_url = 'https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=YOUR_OWN_API_KEY'
# 这里请自行注册OpenWeatherMap获取API Key,或使用其他免费接口
try:
response = requests.get(api_url)
data = response.json()
temp = data['main']['temp'] - 273.15 # 转为摄氏度
weather = data['weather'][0]['description']
print(f"当前天气:{weather},温度:{temp:.1f}°C,适合摸鱼!")
except:
print("网络异常,天气查询失败,但摸鱼不能停!")
提示:为了简化教程,你可以使用https://wttr.in/Beijing?format=3这样的无Key接口,返回简洁文字。但注意使用条件,避免滥用。
第四步:整合定时任务
我们需要让脚本每小时运行一次摸鱼日历,每15分钟查一次天气。使用schedule库实现。
import schedule
def job_calendar():
open_moyu_calendar()
def job_weather():
get_weather()
# 程序入口
if __name__ == "__main__":
# 设置定时任务
schedule.every().hour.do(job_calendar) # 每小时打开一次日历
schedule.every(15).minutes.do(job_weather) # 每15分钟查天气
print("自动摸鱼助手已启动!按 Ctrl+C 停止。")
while True:
schedule.run_pending()
time.sleep(1) # 避免CPU占用过高
运行脚本:在终端执行python mooyu.py。你会看到输出信息,脚本会安静地在后台运行。关闭时按Ctrl+C。
第五步:优化——更隐形的摸鱼方式
直接打开浏览器可能会被IT监控发现。我们可以改为打开一个隐藏的GUI窗口,显示摸鱼日历文本版。这里用tkinter创建一个全屏透明弹窗,并让它快速消失。
import tkinter as tk
def show_moyu_notification():
root = tk.Tk()
root.attributes('-fullscreen', True) # 全屏
root.attributes('-alpha', 0.7) # 半透明
label = tk.Label(root, text="📅 摸鱼时间到!\n今日黄历:宜摸鱼,忌加班",
font=("Arial", 48), bg="black", fg="white")
label.pack(expand=True)
root.after(3000, root.destroy) # 3秒后自动关闭
root.mainloop()
将这个函数替换之前的浏览器调用。这样,屏幕上只会短暂显示一个半透明的提示框,宛如系统通知。同时,你可以用root.attributes('-topmost', True)让它置于最上层。
第六步:高级技巧——自动模拟鼠标移动
很多公司监视员工活跃度,会检查鼠标移动频率。我们可以用pyautogui让鼠标每隔一段时间小幅度移动,假装在工作。
def fake_mouse_movement():
# 随机移动鼠标(范围10像素内)
import random
x, y = pyautogui.position()
new_x = x + random.randint(-10, 10)
new_y = y + random.randint(-10, 10)
pyautogui.moveTo(new_x, new_y, duration=0.2)
print("鼠标假装动了一下...")
# 添加到定时任务中
schedule.every(5).minutes.do(fake_mouse_movement)
注意:不要移动幅度过大,以免触发窗口切换。10像素以内最自然。
最终完整代码
将以上所有功能整合,形成一个完整的摸鱼工具。以下是最终代码(省略了API Key部分,请自行替换):
import time
import random
import webbrowser
import pyautogui
import schedule
import tkinter as tk
import requests # 如不需要天气可删除
# ---- 核心功能 ----
def open_moyu_calendar():
# 使用tkinter弹窗代替浏览器
root = tk.Tk()
root.attributes('-fullscreen', True)
root.attributes('-alpha', 0.6)
root.attributes('-topmost', True)
label = tk.Label(root, text="🕰️ 摸鱼日历:2025-09-20\n宜:睡觉、吃零食\n忌:写代码、开会",
font=("Arial", 48, "bold"), bg="black", fg="lightgreen")
label.pack(expand=True)
root.after(2000, root.destroy) # 2秒后关闭
root.mainloop()
print("日历展示完毕!")
def fake_mouse_movement():
x, y = pyautogui.position()
new_x = x + random.randint(-8, 8)
new_y = y + random.randint(-8, 8)
pyautogui.moveTo(new_x, new_y, duration=0.3)
print(f"鼠标移到 ({new_x}, {new_y})")
# ---- 定时任务 ----
if __name__ == "__main__":
print("=== 自动摸鱼工具 v3.0 ===")
print("项目官网: www.aimoyu.cc")
schedule.every(45).minutes.do(open_moyu_calendar)
schedule.every(5).minutes.do(fake_mouse_movement)
try:
while True:
schedule.run_pending()
time.sleep(1)
except KeyboardInterrupt:
print("\n人工摸鱼模式已退出,老板来了是吗?")
保存并运行。现在,你的电脑会在你离开座位时自动展示摸鱼日历,并让鼠标微微移动,宛如你仍在“努力编码”。当然,本工具仅供学习交流,请勿在重要场合滥用——AI摸鱼工具网的宗旨是:用科技让生活更轻松,而不是让职业生涯更危险。
结语:通过这个教程,你不仅学会了Python的定时任务、GUI弹窗和鼠标模拟,还掌握了一种理性的摸鱼方法论。如果你想要更多摸鱼功能(如万年黄历、APP实时状态),可以访问AI摸鱼工具网,那里有更成熟的解决方案。现在,放心地去摸鱼吧,代码为你保驾护航!
