简体   繁体   English

在特定的一天每小时运行一个脚本

[英]Run a script every hour on a specific day

I want to use the Advanced Python Scheduler ( https://apscheduler.readthedocs.io/en/stable/ ) in order to scrape popular times from Google every hour (40 grocery stores in list markets, from 6:00 to 18:00) on a specific day.我想使用高级 Python 调度程序( https://apscheduler.readthedocs.io/en/stable/ )以便每小时从谷歌(列表市场的 40 家杂货店,从 6:00 到 18:00)抓取热门时间) 在特定的一天。

My code works if I start it manually every hour, but I don't know how to write correct code in order to start the AP Scheduler.如果我每小时手动启动它,我的代码就可以工作,但我不知道如何编写正确的代码来启动 AP 调度程序。 It was running for quite some time, but the resulting data frame was empty.它运行了很长一段时间,但生成的数据框是空的。

I was looking into the manual and other questions, but I couldn't figure out how to write the code.我正在查看手册和其他问题,但我不知道如何编写代码。

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()

So I corrected the dates in my code and tried to run it again.所以我更正了代码中的日期并尝试再次运行它。 But again, it didn't work.但同样,它没有用。 The resulting data frame was empty.结果数据框为空。 The code works if I start it manually (data frame is not empty).如果我手动启动该代码(数据框不为空),则该代码有效。 So if anyone has an idea what is wrong with the code for the scheduler, please let me know.因此,如果有人知道调度程序的代码有什么问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何自动化脚本在特定时间每天运行? - Python: How to automate script to run every day at specific time? 如何在Selenium中运行代码以在一天中的特定时间在python脚本而非cron中执行功能 - How can I run code in selenium to execute a function at a specific hour of the day within the python script not cron 安排 Python 脚本每小时准确运行 - Scheduling Python Script to run every hour accurately 安排一个 python 脚本每小时运行一次 - schedule a python script to run every hour 如何在 GCP 中每天运行 Python 脚本? - How to run a Python script every day in GCP? 如何安排脚本每天运行 Python - How to schedule a script to run every day for Python 在 Heroku 上部署时,每天在特定时间运行函数的 Python 脚本不起作用 - Python script to run a function at a specific time every day does not work when deployed on Heroku 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 在 Python 中安排一个简单的通知脚本每小时运行一次 - Scheduling a simple notification script in Python to run every hour 如何创建一个任务计划程序,使它每天每天运行我的.py文件? - How to create a task scheduler which one run my .py file in every hour in every day?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM