【发布时间】:2020-12-27 18:44:39
【问题描述】:
我是一个非常初学者。我有这门课,我想每天在某个时间从网站获取一些数据(这里是每秒一次,因为我正在测试它)。我想使用计划模块,但我不知道是什么问题。我使用 Pycharm,程序运行时没有输出。
import requests
import time
import schedule
class Bot:
def __init__(self):
self.url = 'https://www.website.com'
self.params = {
...
}
self.headers = {
...
}
self.orders = []
def fetchCurrenciesData(self):
r = requests.get(url=self.url, headers=self.headers, params=self.params).json()
return r['data']
schedule.every(5).seconds.do(Bot)
while True:
schedule.run_pending()
time.sleep(1)
我也尝试过这样做:
impactBot = Bot()
schedule.every(5).seconds.do(impactBot())
while True:
schedule.run_pending()
time.sleep(1)
但在这里我得到一个错误,他们说“Bot 对象不可调用”。我做错了什么?
【问题讨论】:
-
你只是在调用类构造函数。您永远不会调用实际发出请求的方法。
-
好的,我可以按照其他用户的建议用“schedule...do.(impactBot.fetchCurrenciesData)”来调用它,但程序仍然没有输出运行
-
如果你想显示输出为什么你不在方法
fetchCurrenciesData中加入打印词干
标签: python python-3.x python-requests schedule python-class