【问题标题】:Python schedule :missing 1 required positional argument?Python 时间表:缺少 1 个必需的位置参数?
【发布时间】:2019-03-04 04:47:09
【问题描述】:

这是我的代码。

from API.helpers import get_weather_data, json_to_df, create_dict
import schedule, time

URL = 'https://pm1aapplicantsdata.blob.core.windows.net/databases/CitiesWeather/CitiesWeather.csv'
columns = ["name","sys.country","main.temp",
           "main.humidity","main.pressure",
           "visibility", "wind.speed"]

def weather_api(URL):
    dict = create_dict(URL)
    for city, code in dict.items():
        data = get_weather_data(city, code)
        json_to_df(data, columns)

schedule.every(10).minutes.do(weather_api())
while True:
    schedule.run_pending()
    time.sleep(1)

我想做的是定期运行它。但是,我收到一条错误消息"weather_api() missing 1 required positional argument: 'URL'"。我试图将它传递到时间表schedule.every(10).minutes.do(weather_api(URL)),但后来我收到the first argument must be callable 错误。同样在这种情况下...

def weather_api():
    URL = 'https://pm1aapplicantsdata.blob.core.windows.net/databases/CitiesWeather/CitiesWeather.csv'
    dict = create_dict(URL)
    for city, code in dict.items():
        data = get_weather_data(city, code)
        json_to_df(data, columns)

schedule.every(10).minutes.do(weather_api())
while True:
    schedule.run_pending()
    time.sleep(1)

...错误仍然存​​在。我之前尝试过使用 Advanced Python Scheduler,但问题是一样的。否则我的脚本工作正常。我做错了什么?

【问题讨论】:

    标签: python python-3.x scheduled-tasks schedule


    【解决方案1】:

    我不确定你应该如何在.do() 中发送参数,但是关于第二个异常,你应该像这样运行它:

    schedule.every(10).minutes.do(weather_api)
    

    而不是weather_api()

    .do() 需要一个函数作为参数,而weather_api 是您需要发送的函数,而不是它返回的数据(通过向其添加())。

    【讨论】:

      【解决方案2】:

      the source code of schedule

      您可能想使用schedule.every(10).minutes.do(weather_api, URL)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-20
        • 2020-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-13
        相关资源
        最近更新 更多