1

我是一个非常初学者。我有这门课,我想每天在某个时间从网站获取一些数据(这里是每秒一次,因为我正在测试它)。我想使用计划模块,但我不知道是什么问题。我使用 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 对象不可调用”。我做错了什么?

4

2 回答 2

0

您需要为初始化对象调用类,然后调用对象类的方法。要解决它,请按照我的示例:

ClassObj = Bot()
# Call method fetchCurrenciesData
ClassObj.fetchCurrenciesData()

# or 
# Singal line
Bot().fetchCurrenciesData()

以下是您的代码示例。

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

while True:
    schedule.run_pending()
    time.sleep(1)
于 2020-09-09T10:49:42.940 回答
-1

尝试这个:

impactBot = Bot()

schedule.every(5).seconds.do(impactBot.fetchCurrenciesData)

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

schedule....do()需要一个可调用的。

于 2020-09-09T10:45:32.447 回答