无法让计数器在 tweepy 中工作
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'])
我是 tweepy 的新手,我想做的第一件事就是制作一个程序来扫描所有推文中的某些词。在我尝试为该词的实例数添加一个计数器之前,一切都运行良好。无论我在哪里分配计数器,我总是会收到“UnboundLocalError: local variable 'counter' referenced before assignment”错误。我应该在这个程序中的哪里分配计数器?
假设上面的 "can't" 不在您的代码中,如果可能的话更新您的问题以省略“'”,因为它会扰乱可读性或更改为其他占位符文本。
如错误所示,您尚未为计数器分配值,方法 "on_status" 将尝试递增计数器,但这仅是方法本地的,而不是对象,因此它失败了。
def on_status(self, status):
#prints status text. can be replaced with a counter probably.
counter = counter + 1
print(status.text)
您应该在 init 方法中初始化计数器,然后改用 self.counter。
添加
...
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
...
修改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)
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'])
我是 tweepy 的新手,我想做的第一件事就是制作一个程序来扫描所有推文中的某些词。在我尝试为该词的实例数添加一个计数器之前,一切都运行良好。无论我在哪里分配计数器,我总是会收到“UnboundLocalError: local variable 'counter' referenced before assignment”错误。我应该在这个程序中的哪里分配计数器?
假设上面的 "can't" 不在您的代码中,如果可能的话更新您的问题以省略“'”,因为它会扰乱可读性或更改为其他占位符文本。
如错误所示,您尚未为计数器分配值,方法 "on_status" 将尝试递增计数器,但这仅是方法本地的,而不是对象,因此它失败了。
def on_status(self, status):
#prints status text. can be replaced with a counter probably.
counter = counter + 1
print(status.text)
您应该在 init 方法中初始化计数器,然后改用 self.counter。
添加
...
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
...
修改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)