简体   繁体   English

每天用 Python 下载数据

[英]Downloading data daily with Python

Hi I created a function to download data from a crypto exchange and I would like to automate this process so that I don't need to do it myself.嗨,我创建了一个从加密交换下载数据的函数,我想自动化这个过程,这样我就不需要自己做。 I looked at schedule but it throws me an error.我查看了时间表,但它给我带来了错误。 here is the code:这是代码:

symbols = ['BTCUSDT', 'ETHUSDT', 'ADAUSDT', 'BNBUSDT', 'DOTUSDT', 'XRPUSDT', 'UNIUSDT', 'LTCUSDT', 'LINKUSDT',
       'BCHUSDT', 'XLMUSDT', 'LUNAUSDT', 'DOGEUSDT', 'VETUSDT','ATOMUSDT', 'AAVEUSDT', 'FILUSDT', 'AVAXUSDT',
       'TRXUSDT', 'EOSUSDT', 'SOLUSDT', 'IOTAUSDT', 'XTZUSDT', 'NEOUSDT', 'CHZUSDT', 'DAIUSDT', 'SNXUSDT',
       'SUSHIUSDT', 'EGLDUSDT', 'ENJUSDT', 'ZILUSDT', 'MATICUSDT', 'MKRUSDT', 'COMPUSDT', 'BATUSDT', 'ZRXUSDT',
       'RSRUSDT']

for symbol in symbols:
   schedule.every().day.at("21:00").do(get_all_binance(timeframe_folder = '1h',symbol, kline_size = '1h', save = True))
   schedule.every().day.at("21:00").do(get_all_binance(timeframe_folder = '30m',symbol, kline_size = '30m', save = True))
   schedule.every().day.at("21:00").do(get_all_binance(timeframe_folder = '4h',symbol, kline_size = '4h', save = True))
   schedule.every().day.at("21:00").do(get_all_binance(timeframe_folder = '12h',symbol, kline_size = '12h', save = True))
   schedule.every().day.at("21:00").do(get_all_binance(timeframe_folder = '1d',symbol, kline_size = '1d', save = True))

while True:
   schedule.run_pending()
   time.sleep(5)

And here is the error:这是错误:

Traceback (most recent call last):
File "/Users/louis/Desktop/Python_projects/streamlit_apps/dashboards/collect_data.py", line 70, in <module>
schedule.every().day.at("21:05").do(get_all_binance('1h',symbol, '1h', save = True))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/schedule/__init__.py", line 625, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable

How can I fix this ?我怎样才能解决这个问题 ? and are there any other solutions ?还有其他解决方案吗? Thank you !谢谢 !

The first argument should be callable, meaning it should be the actual function object and not the return value of the function.第一个参数应该是可调用的,这意味着它应该是实际的函数对象而不是函数的返回值。 What you are doing is this:你在做什么是这样的:

schedule.every().day.at("21:00").do(some_random_func())

What you should actually be doing is this:你实际上应该做的是:

schedule.every().day.at("21:00").do(some_random_func)

Notice how there are no brackets in the second one.注意第二个中没有括号。 This is because I am passing the actual function object as an argument.这是因为我将实际的函数对象作为参数传递。 By having the brackets present, Python will call the function, and have the return value of the function passed as an argument instead, and because the return value of your function probably isn't a function, it throws the error stating that the value passed as the first argument was not a function as it expected.通过使用方括号,Python 将调用该函数,并将该函数的返回值作为参数传递,并且由于您的函数的返回值可能不是函数,所以它会抛出错误,指出传递的值因为第一个参数不是预期的函数。

The function you are passing (get_all_binance) has arguments, however, so you can either use lambdas or pass the arguments after the function:但是,您传递的函数 (get_all_binance) 具有参数,因此您可以使用 lambdas 或在函数后传递参数:

Either this:要么这样:

schedule.every().day.at("21:00").do(get_all_binance, ...args)

or this:或这个:

schedule.every().day.at("21:00").do(lambda: get_all_binance(...args))

A lambda is just an anonymous function, so "lambda: get_all_binance(...args)" returns a function that does the actions and returns the value get_all_binance. lambda 只是一个匿名函数,因此“lambda: get_all_binance(...args)”返回一个执行操作并返回值 get_all_binance 的函数。 So all in all each line will look like this:所以总而言之,每一行看起来像这样:

schedule.every().day.at("21:00").do(lambda: get_all_binance(timeframe_folder = '1h',symbol, kline_size = '1h', save = True))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM