【问题标题】:Tweepy 4.0: "'Listener' object has no attribute 'running'" error?Tweepy 4.0:“'Listener'对象没有属性'running'”错误?
【发布时间】:2021-08-29 04:30:26
【问题描述】:

我正在尝试将基本的流式 Twitter 机器人转换为 Tweepy 4.0 alpha,以便利用一些较新的功能。但是,我什至无法开始使用描述新流协议的文档。

我已将我的程序简化为一个测试用例,但运行它仍然会给出错误AttributeError: 'MyStreamListener' object has no attribute 'running'。有人能指出我的问题在哪里吗?关于这个特定错误似乎没有任何其他问题,我试图密切关注文档。由于缺少示例,我不得不做出一些猜测——例如,连接到流需要一组不同的参数,而不是连接到常规 api,因此有两个 get_auth 调用——但目前我是更感兴趣的是让它工作而不是让事情变得超级干净。

#!/usr/bin/env python3

import tweepy
import json
import configparser
import os


def _read_config():
    config = configparser.ConfigParser()
    script_dir = os.path.dirname(__file__)
    config_file = os.path.join(script_dir, '../bot-config.ini')
    config.read(config_file)
    return config


def _get_api_auth(config):
    auth = tweepy.OAuthHandler(
        config['auth']['consumer_key'], config['auth']['consumer_secret'])
    auth.set_access_token(config['auth']['token'],
                          config['auth']['token_secret'])
    return auth


def _get_stream_auth(config):
    return (config['auth']['consumer_key'], config['auth']['consumer_secret'],
            config['auth']['token'], config['auth']['token_secret'])


class MyStreamListener(tweepy.Stream):

    def __init__(self, stream_auth, api):
        super(tweepy.Stream, self).__init__()
        self._api = api
        self._tweet_count = 0

    def on_data(self, data):
        tweet = json.loads(data)

        if 'text' not in tweet:
            return

        self._tweet_count += 1

        if tweet['truncated']:
            text = tweet['extended_tweet']['full_text']
        else:
            text = tweet['text']

        if 'Rumplestiltskin' in text:
            try:
                self._api.retweet(tweet['id'])
                print('Tweeted id %d!', tweet['id'])
            except tweepy.TweepError as err:
                print('Unable to tweet %d:', tweet['id'])
                print(err.reason)

        if (self._tweet_count % 10000) == 0:
            print('Processed %s tweets', self._tweet_count)


def run():

    config = _read_config()

    api_auth = _get_api_auth(config)
    api = tweepy.API(api_auth)
    stream_auth = _get_stream_auth(config)

    twitterStream = MyStreamListener(stream_auth, api)
    twitterStream.sample()


if __name__ == '__main__':
    run()


【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    呸,这基本上只是一个简单的 Python 错误,正如 Discord 服务器中的某个人所指出的那样。我会把它留在这里,以防其他人犯同样的错误;基本上我需要单独调用所有 auth 的东西,所以调用行应该是:

     twitterStream = MyStreamListener(config['auth']['consumer_key'],
                                           config['auth']['consumer_secret'],
                                           config['auth']['token'],
                                           config['auth']['token_secret'],
                                           api)
    

    我还弄错了super 语法;应该是:

    def __init__(self, cons_key, cons_secret, token, token_sec, api):
        super().__init__(cons_key, cons_secret, token, token_sec)
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-19
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2014-05-14
      • 2016-04-07
      • 1970-01-01
      相关资源
      最近更新 更多