LINE Notify 传送屏幕截图
这篇文章会使用 Python 的 Requests 和 pyautogui 函数库,实作一个会自动进行屏幕截图,并在截图后将截图透过 LINE Notify 传送出去的程序。
注意,LINE Notify 服务已于 2025 年停止,本篇教学仅供参考使用。
执行 pyautogui 会由程控电脑鼠标、键盘或画面,因此所以请使用本机环境 ( 参考:使用 Python 虚拟环境 ) 或使用 Anaconda Jupyter 进行实作 ( 参考:使用 Anaconda ) 。
文章导读
在实作本篇范例前,请先阅读“发送 LINE Notify 通知”和“定时自动屏幕截图”两篇文章,并预先取得 LINE notify 的权杖。
使用 LINE Notify 传送屏幕截图
下方的程序执行后,会使用 pyautogui 函数库进行截图并储存为 test.png,接着使用 requests 函数库以 POST 的方式,将图片传送到指定的 LINE 聊天频道里。
注意,LINE Notify 服务已于 2025 年停止,本篇教学仅供参考使用。
import pyautogui
import requests
myScreenshot = pyautogui.screenshot() # 截圖
myScreenshot.save('./test.png') # 儲存為 test.png
url = 'https://notify-api.line.me/api/notify'
token = '你的權杖'
headers = {
'Authorization': 'Bearer ' + token # 設定 LINE Notify 權杖
}
data = {
'message':'測試一下!' # 設定 LINE Notify message ( 不可少 )
}
image = open('./test.png', 'rb') # 以二進位方式開啟圖片
imageFile = {'imageFile' : image} # 設定圖片資訊
data = requests.post(url, headers=headers, data=data, files=imageFile) # 發送 LINE Notify
加入时间信息、定时截图
修改上方的程序,加入 time 内建函数库,将消息内容更换为截图当下的时间,使用 sleep 与 for 循环搭配,将程序使用“函数”包装,就能做到定时截图和发送 LINE Notify 的功能。
import pyautogui
import requests
import time
# 定義截圖的函式
def screenshot():
myScreenshot = pyautogui.screenshot()
myScreenshot.save('./test.png')
t = time.time() # 取得到目前為止的秒數
t1 = time.localtime(t) # 將秒數轉換為 struct_time 格式的時間
now = time.strftime('%Y/%m/%d %H:%M:%S',t1) # 輸出為特定格式的文字
sendLineNotify(now) # 執行發送 LINE Notify 的函式,發送的訊息為時間
# 定義發送 LINE Notify 的函式
def sendLineNotify(msg):
url = 'https://notify-api.line.me/api/notify'
token = '你的權杖'
headers = {
'Authorization': 'Bearer ' + token
}
data = {
'message':msg
}
image = open('./test.png', 'rb')
imageFile = {'imageFile' : image}
data = requests.post(url, headers=headers, data=data, files=imageFile)
# 使用for 迴圈,每隔五秒截圖發送一次
for i in range(5):
screenshot()
time.sleep(5)
小结
这个范例的功能,好像可以应用在“监控小孩子使用电脑的状况”( 定时发送图片,看看小孩都在干嘛 ),但仍然要注意的是,在没有得到同意的状况下进行监控,会触犯刑法第 315 之 1 条呦~
微信扫码关注
抖音扫码关注