簡體   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