【问题标题】:Python Tweepy 4.4 streamlistener attribute not found?找不到 Python Tweepy 4.4 streamlistener 属性?
【发布时间】:2022-10-16 21:47:48
【问题描述】:

我正在尝试在来自特定推特帐户的推文中收听某个角色。

我得到一个编译时错误。

这是代码的相关部分:

import tweepy

class MyStreamListener(tweepy.streaming.StreamListener):
    
    def on_status(self, status):
        print(status.text)
  
def main():
    api = twitter_authentication() #this function does the necessary twitter auth

    myStreamListener = MyStreamListener()
    myStream = tweepy.streaming.Stream(auth=api.auth, listener=myStreamListener)
    myStream.filter(track=["test"], follow=["123456"])

当我在 pycharm 中运行它时,我收到以下错误:

line 68, in <module>
class MyStreamListener(tweepy.streaming.StreamListener):
AttributeError: module 'tweepy.streaming' has no attribute 'StreamListener'

【问题讨论】:

    标签: python stream tweepy


    【解决方案1】:

    Tweepy v4.0.0 最近发布了,它将 StreamListener 合并到 Stream 中。

    您应该将代码更新为 Stream 的子类,或者您也可以将版本更改为 v3.10.0 。

    我想你可以做你想做的事情:

    import tweepy
    
    class MyStreamListener(tweepy.Stream):
    
        def on_status(self, status):
            print(status.text)
    
        def on_error(self, status_code):
            if status_code == 420:  # end of monthly limit rate (500k)
                return False
    
    
    stream = MyStreamListener('consumer_key',
                          'consumer_secret',
                          'access_token',
                          'access_token_secret')
    
    stream.filter(track=["test"], follow=["123456"])
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2016-11-11
      • 1970-01-01
      • 2018-01-06
      • 2018-08-13
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多