Python 时间表:缺少 1 个必需的位置参数?
Python schedule :missing 1 required positional argument?
这是我的代码。
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,但问题是一样的。否则我的脚本工作正常。我做错了什么?
我不确定你应该如何在 .do()
中发送参数,但关于第二个例外,你应该 运行 像这样:
schedule.every(10).minutes.do(weather_api)
而不是 weather_api()
。
.do()
需要一个函数作为参数,weather_api
是您需要发送的函数,而不是它 returns 的数据(通过添加 ()
到它)。
您可能想使用 schedule.every(10).minutes.do(weather_api, URL)
这是我的代码。
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,但问题是一样的。否则我的脚本工作正常。我做错了什么?
我不确定你应该如何在 .do()
中发送参数,但关于第二个例外,你应该 运行 像这样:
schedule.every(10).minutes.do(weather_api)
而不是 weather_api()
。
.do()
需要一个函数作为参数,weather_api
是您需要发送的函数,而不是它 returns 的数据(通过添加 ()
到它)。
您可能想使用 schedule.every(10).minutes.do(weather_api, URL)