最近我开始使用 python 并找到了一个很酷的脚本来监控网站的变化,然后在内容与我要查找的内容匹配时向一个或多个人发送电子邮件。我知道有更好的方法来制作它,但到目前为止效果很好,如果你们中的一些人可能需要,我会分享它:
# Import requests (to download the page)
import requests
# Import BeautifulSoup (to parse what we download)
from bs4 import BeautifulSoup
# Import Time (to add a delay between the times the scape runs)
import time
# Import smtplib (to allow us to email)
import smtplib
# This is a pretty simple script. The script downloads the homepage of VentureBeat, and if it finds some text, emails me.
# If it does not find some text, it waits 60 seconds and downloads the homepage again.
# while this is true (it is true by default),
while True:
# set the url as you please,
url = "google.com"
# set the headers like we are a browser,
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36'}
# download the homepage
response = requests.get(url, headers=headers)
# parse the downloaded homepage and grab all text, then,
soup = BeautifulSoup(response.text, "lxml")
# if the number of times the word "Google" occurs on the page is less than 1,
if (str(soup).find("Google") == -1):
# wait 60 seconds,
time.sleep(60)
# continue with the script,
continue
# but if the word "Google" occurs any other number of times,
else:
#print(soup)
# create an email message with just a subject line,
msg = 'Subject: A prefeitura de Ilhabela postou um edital novo, VAI LA VER!'
# set the 'from' address,
fromaddr = 'testmail@gmail.com'
# set the 'to' addresses,
toaddrs = ['anothertestmail@gmail.com']
# setup the email server,
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
# add my account login name and password,
server.login('tesmail@gmail.com', 'TheTest')
# Print the email's contents
print('From: ' + fromaddr)
print('To: ' + str(toaddrs))
print('Message: ' + msg)
# send the email
server.sendmail(fromaddr, toaddrs, msg)
# disconnect from the server
server.quit()
break
现在我想知道:我怎样才能让这个脚本 24/7 全天候运行?谷歌和亚马逊可能对此有解决方案,但我太缺乏经验,无法找到答案。你们能帮我吗?
答案 0 :(得分:0)
如果您使用的是 Windows,则可以使用 Task Scheduler 在您自己的计算机上运行它。