我对tweepy很陌生,现在下面的代码只将结果输出到屏幕上,但我想做的是:将每个结果传递给变量:
例如:
tweet = Twitter().start()
print tweet
>> justinbieber
所以我的问题是我不知道如何一次返回一个值。
现在我有了这个,下面的代码将永远存在,在我按下 Ctrl + C 后它不会将结果存储在 tt 中:
>>> import twitter_streaming_tweepy as t
>>> tt = t.Twitter().start()
alwaysupportjus
Madam_Rauhl
这是代码:
import sys
import tweepy
output = []
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
print status.author.screen_name
output.append(status.author.screen_name)
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
class Twitter():
def __init__(self):
consumer_key="*******"
consumer_secret="*******"
access_key = "*******"
access_secret ="*******"
self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
self.auth.set_access_token(access_key, access_secret)
self.api = tweepy.API(self.auth)
def start(self):
l = CustomStreamListener()
stream = tweepy.streaming.Stream(self.auth,l)
stream.filter(follow=None, track=['justinbieber'])
return output
if __name__ == "__main__":
Twitter().start()