【问题标题】:How can to keep a Python script running on the web server/hosting?如何让 Python 脚本在 Web 服务器/主机上运行?
【发布时间】:2021-03-06 17:51:30
【问题描述】:

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

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

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

    【讨论】:

    • 这是我的 B 计划。我需要云上的东西,服务将始终启动并运行。
    • 如果您使用 Linux VPS,则在 Linux 中使用 crontab。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2011-09-30
    • 2019-11-17
    • 1970-01-01
    • 2012-03-13
    • 2018-01-30
    • 2015-02-05
    相关资源
    最近更新 更多