在特定日期每小时运行一个脚本

时间:2021-01-30 06:37:57

标签: python scheduled-tasks apscheduler

我想使用高级 Python 调度程序 (https://apscheduler.readthedocs.io/en/stable/) 以便在特定日期每小时(列表市场中的 40 家杂货店,从 6:00 到 18:00)从 Google 抓取热门时间。

如果我每小时手动启动它,我的代码就可以工作,但我不知道如何编写正确的代码来启动 AP 调度程序。它运行了很长时间,但结果数据框是空的。

我正在查看手册和其他问题,但我不知道如何编写代码。

from apscheduler.schedulers.blocking import BlockingScheduler

def job():
    for market in markets:
        data = livepopulartimes.get_populartimes_by_address(market)
        current_popularity_ = pd.DataFrame([[date.today().strftime("%Y-%m-%d"), 
                           date.today().strftime("%A"),
                           datetime.now().strftime("%H:%M:%S"),
                           pd.DataFrame.from_dict(data, orient='index').loc['current_popularity'].values[0]
                          ]],
             columns=['date','day','time','value'])
        current_popularity_['market'] = market
        current_popularity = current_popularity.append(current_popularity_)

sched = BlockingScheduler()

sched.add_job(
    job, 
    trigger='cron',
    hour='06-18',
    start_date = '2021-02-04',
    end_date = '2021-02-04'
)

sched.start()

1 个答案:

答案 0 :(得分:1)

所以我更正了代码中的日期并尝试再次运行它。 但是,它再次不起作用。结果数据框为空。 如果我手动启动该代码(数据框不为空),则该代码有效。 因此,如果有人知道调度程序的代码有什么问题,请告诉我。