为什么计划和请求库不';你不能在Python中使用这个类吗?

为什么计划和请求库不';你不能在Python中使用这个类吗?,python,python-3.x,python-requests,schedule,python-class,Python,Python 3.x,Python Requests,Schedule,Python Class,我是个初学者。我有一节课,我想每天在某个时间从网站上获取一次数据(这里是每秒钟,因为我在测试它)。我想使用schedule模块,但我不知道问题出在哪里。我使用Pycharm,程序运行时没有输出 import requests import time import schedule class Bot: def __init__(self): self.url = 'https://www.website.com' self.params = {

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

试试这个:

impactBot = Bot()

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

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

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

您需要为init对象调用类,然后调用对象类的方法。 要解决此问题,请遵循我的示例:

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)

您只能调用类构造函数。您从未调用实际发出请求的方法。好的,我可以按照另一个用户的建议使用“schedule…do.(impactBot.fetchcurrencesdata)”来调用它,但程序运行时仍然没有输出如果您想显示输出,为什么不在方法
fetchcurrencesdata
Done中放入打印项,但这些程序仍在继续运行,没有任何问题output@User147你为什么期望产出?我没有看到任何对
print
的调用会产生一些输出。您是否只想每隔5秒“查看”
r[“数据”]
?然后将
print(r[“data”])
放入
fetchcurrencesdata
方法中(而不是返回)太好了,我也有同样的问题,花了几个小时在谷歌上搜索解决方案,谢谢你救了我一天:)谢谢,但我仍然得到了“TypeError:第一个参数必须是可调用的”错误在我执行了您在Pycharm中编写的操作之后,如果有帮助,我也会从计划包文件中得到此错误:/…/site packages/schedule/\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Bot().fetchcurrencesdata()
只执行一次,结果(无法调用)将被传递给
.do