简体   繁体   English

无法在柜台工作

[英]Can't get a counter working in tweepy

from tweepy import OAuthHandler

import tweepy
from tweepy import StreamListener
from tweepy import Stream


import time



consumer_key = 'super secret consumer key'
consumer_secret = 'shhhhh can't tell anyone this!'
access_token = 'hmmmmmmmmmmmmm'
access_secret = 'arrays should start at 0'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)
print('')
print('starting...')
time.sleep(3)

class MySteamListener(tweepy.StreamListener):







    def on_status(self, status):
        #prints status text. can be replaced with a counter probably.
        counter = counter + 1
        print(status.text)


    def on_error(self, status_code):
        if status_code == 420:
            print('420 error')
            #Ends stream in case of rate limiting
            return False


mySteamListener = MySteamListener()

myStream = tweepy.Stream(auth = api.auth, listener = mySteamListener)

myStream.filter(track = ['Warriors'])

I'm new to tweepy, and the first thing I'm trying to do is make a program that scans all tweets for certain words. 我是tweepy的新手,我要尝试做的第一件事是制作一个程序,该程序扫描所有tweet中的某些单词。 Everything was working well until I tried to add a counter for the number of instances for that word. 一切工作正常,直到我尝试为该单词的实例数添加一个计数器。 No matter where I assign the counter, I always get a 'UnboundLocalError: local variable 'counter' referenced before assignment' error. 无论我在何处分配计数器,我总是会收到“ UnboundLocalError:分配前引用本地变量” counter”错误。 Where should I assign the counter in this program? 在该程序中我应该在哪里分配计数器?

Assuming the "can't" above is not really in your code, if possible update your question to omit the "'" as it messes up the readability or change to some other placeholder text. 假设上面的“不能”实际上不在您的代码中,如果可能,请更新您的问题以省略“'”,因为它会破坏可读性或更改为其他占位符文本。

As the error indicates, you have not assigned a value yet to counter, the method "on_status" will try to increment counter, but this is only local to the method, not the object, thus it fails. 如错误所示,您尚未分配要计数的值,方法“ on_status”将尝试递增计数器,但这仅是方法的本地对象,而不是对象的本地对象,因此失败。

def on_status(self, status):
        #prints status text. can be replaced with a counter probably.
        counter = counter + 1
        print(status.text)

You should initialize the counter in an init method and then use self.counter instead. 您应该使用init方法初始化计数器,然后改用self.counter。

Add

...
class MySteamListener(tweepy.StreamListener):

    def __init__(self):
        # Not sure if necessary, to make this work, but you could
        # Initialize the inherited class as well (this may work only in Python 3)
        # super().__init__()
        self.counter = 0
...

Modify on_status to 修改on_status为

def on_status(self, status):
        #prints status text. can be replaced with a counter probably.
        self.counter = self.counter + 1
        # Can be written as 'self.counter += 1'
        print(status.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM