简体   繁体   中英

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

Recently I started working with python and found a cool script to monitor the changes of a website minute after minute and then send an email to one or more people if the content matches what I'm looking for. I know there are better ways to make it, but so far it's working great and i'll share it in case some of you may need:

# 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

Now I wonder: how can I keep this scrip running 24/7? Google and Amazon probably have solutions for this, but I'm too unexperienced to find the answers. Could you guys help me with that?

You can run it on your own computer using Task Scheduler if you're using Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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