使用计划任务任务类型为“shell脚本”脚本内容为填入py脚本,解释器选为python,但任然会以sh脚本运行,导致错误。
方便贴一下 py 脚本吗?
#!/usr/bin/env python3
import subprocess
import sys
import io
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
try:
from PIL import Image, ImageChops
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "Pillow"])
from PIL import Image, ImageChops
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=zh-CN')
chrome_options.add_argument('--font-render-hinting=medium')
urls = [
"https://163.com",
"",
# 其他网址
]
threshold = 50
screenshot_dir = "/docker/python_script/163"
to_email = "111111@qq.com" # 替换为接收邮箱
def get_screenshot(driver, url):
driver.get(url)
max_height = 10000
total_height = driver.execute_script("return document.body.scrollHeight")
if total_height > max_height:
total_height = max_height
driver.set_window_size(1200, total_height)
png = driver.get_screenshot_as_png()
img = Image.open(io.BytesIO(png))
return img
def images_are_different(img1, img2, threshold):
diff = ImageChops.difference(img1, img2)
if diff.getbbox() is None:
return False
diff_sum = sum(diff.histogram())
return diff_sum > threshold
def send_email(subject, content, to_email):
email_text = f"Subject: {subject}\n\n{content}"
process = subprocess.Popen(['msmtp', to_email], stdin=subprocess.PIPE)
process.communicate(email_text.encode('utf-8'))
def main():
driver = webdriver.Chrome(options=chrome_options)
try:
if not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
for url in urls:
try:
current_img = get_screenshot(driver, url)
file_name = url.replace("https://", "").replace("http://", "").replace("/", "_") + ".png"
screenshot_path = os.path.join(screenshot_dir, file_name)
if os.path.exists(screenshot_path):
last_img = Image.open(screenshot_path)
if images_are_different(last_img, current_img, threshold):
subject = f"[网页色彩变化提醒] {url} 页面色彩变化"
content = f"网址 {url} 的页面检测到色彩变化,请及时查看。"
send_email(subject, content, to_email)
print(f"{url} 检测到色彩变化,邮件已发送。")
else:
print(f"{url} 页面色彩无变化。")
else:
print(f"{url} 无历史截图,首次保存当前截图。")
current_img.save(screenshot_path)
except Exception as e:
print(f"{url} 监控异常: {e}")
finally:
driver.quit()
if __name__ == '__main__':
main()
好的,我先本地复现一下
计划任务输出麻烦也贴一下,执行器选择自定义,然后输入 python3