Python 我想用Selenium每隔15分钟ping一个网站,以确保网站正常运行
我需要检查一个特定网站的可用性。我正在尝试编写一个Python脚本,每15分钟ping一个网站。如果网站关闭,我希望它输出网站不可用,并发送电子邮件通知用户网站关闭 我对python非常陌生,不知道从哪里开始Python 我想用Selenium每隔15分钟ping一个网站,以确保网站正常运行,python,selenium,Python,Selenium,我需要检查一个特定网站的可用性。我正在尝试编写一个Python脚本,每15分钟ping一个网站。如果网站关闭,我希望它输出网站不可用,并发送电子邮件通知用户网站关闭 我对python非常陌生,不知道从哪里开始 from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options, executable_path="C:/Users/TX394UT/Desktop/Web_Bot_Project/chromedriver.exe")
driver.get("https://www.google.com/")
print(driver.title)
driver.close()
driver.quit()
print("Completed Sucessfully")
如果无法访问此网站,我希望它向我发送电子邮件通知。我希望脚本每15分钟运行一次。安排任务在后台运行
import requests
import time
while True:
try:
requests.get('https://www.google.com/')
# insert code that runs when site is up here
except:
# insert code that runs when site is down here
time.sleep(900)
在Linux上
在Linux上调度后台脚本的最简单方法是使用cron和crontab生成器,如
例如,如果每15分钟运行一个python脚本,它将如下所示:
15 * * * * python /home/user/scripts/examplescript.py >/dev/null 2>&1
在窗户上
在windows上,您可以使用内置的windows任务计划程序,另一部分对此进行了说明
此外,使用selenium检查网站是否正常运行也不是很明智,请尝试检查哪个库是发出HTTP请求的非常好的库。我喜欢使用请求库来检测网站是否关闭。 使用while循环和time.sleep函数每15分钟运行一次代码
import requests
import time
while True:
try:
requests.get('https://www.google.com/')
# insert code that runs when site is up here
except:
# insert code that runs when site is down here
time.sleep(900)
你为什么要用硒来做这个?@DanielRoseman我不同意你的看法。。。也许你可以建议一个更好的替代方案?WebsitePulse或Pingdom工具可能比Selenium更适合。如果你想使用Selenium,我会设置Jenkins,每15分钟运行一次作业,并发送一封包含结果的电子邮件。例如,当你点击谷歌时,我只想确认你的标识是否存在。这个解决方案的问题是你需要打开电子邮件才能看到结果。您可以设置一个规则,在电子邮件未通过时忽略它并删除它。你的问题确实更适合Pingdom或网站。不要在这个简单的任务中使用selenium