【问题标题】:How to make my function run every hour?如何让我的函数每小时运行一次?
【发布时间】:2017-02-03 19:05:03
【问题描述】:

在一些在线 tuts (Bucky) 的帮助下,我设法编写了一个简单的网络爬虫,它只检查网页上是否有一些文本。然而,我想做的是让代码每小时运行一次。我假设我还需要托管代码,所以它会这样做吗?我做了一些研究,但似乎无法找到每小时运行它的正确方法。 这是我目前得到的代码:

import requests
from bs4 import BeautifulSoup

def odeon_spider(max_pages):
    page = 1
    while page <= max_pages:
        url = "http://www.odeon.co.uk/films/rogue_one_a_star_wars_story/16038/" + str(page) #stores url in variable
        source_code = requests.get(url) #gets url and sets it as source_code variable
        plain_text  = source_code.text #stores plain text in plain_text variable
        soup = BeautifulSoup(plain_text, "lxml") #create beautifulsoup object
        div_content = soup.findAll("div", {"class": "textComponent"}) #finds all divs with specific class
        for x in div_content:
            find_para = str(x.find('p').text) #finds all paragraphs and stores them in variable
            text_to_search = "Register to be notified" #set text to search to variable
            if text_to_search in find_para: #checks if text is in find_para
                print("No tickets")
            else:
                print("Tickets")
        page += 1


odeon_spider(1)

谢谢!

【问题讨论】:

  • Linux 机器使用crontab
  • 在 Linux 上使用 cronjobs,在 windows 上你可以使用 taskscheduler

标签: python function web scraper


【解决方案1】:

最简单的方法是这样的:

import time

while True:
    call_your_function()
    time.sleep(3600)

如果您想在 Linux 上执行此操作,只需键入

nohup python -u your_script_name &

然后你的脚本将作为一个进程运行。(如果你不杀死它,它会继续运行而不会挂断。)

【讨论】:

  • 我修复了您的 Python 代码,因为 Python 中没有 Whilewhile 并且括号也不应该存在。我在 Linux shell 中添加了&amp;,否则 shell 将无法取回控制权。哦,你的代码还可以。我仍然会使用 cron。
猜你喜欢
  • 1970-01-01
  • 2015-11-20
  • 2020-09-11
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 2019-10-13
相关资源
最近更新 更多