繁体   English   中英

如何保持 Python 脚本在 web 服务器/主机上运行?

[英]How can to keep a Python script running on the web server/hosting?

最近,我开始使用 python 并找到了一个很酷的脚本来监控网站一分钟又一分钟的变化,然后如果内容与我正在寻找的内容匹配,则将 email 发送给一个或多个人。 我知道有更好的方法来制作它,但到目前为止它工作得很好,我会分享它以防你们中的一些人可能需要:

# 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 全天候运行? 谷歌和亚马逊可能对此有解决方案,但我太缺乏经验,无法找到答案。 你们能帮我解决这个问题吗?

如果您使用的是 Windows,您可以使用任务计划程序在您自己的计算机上运行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM