【发布时间】:2018-08-13 20:30:08
【问题描述】:
我有以下代码来创建一个 Stream Listener 并从 Twitter API 获取推文。
class MyStreamListener(tweepy.StreamListener):
#class constructor
def __init__(self,api=None):
super(MyStreamListener,self).__init__()
#creates class variables and instantiates to file and number
self.num_tweets = 0
self.file = open("tweets.txt",'w')
#function to collect tweets and add to file
def on_status (self,status):
tweet = status._json
print("Tweet: " + tweet)
print("STATUS: " + status.text)
self.file.write(json.dumps(tweet)+'\n')
print(status)
self.num_tweets+=1
if self.num_tweets < 100:
return True
else:
return False
self.file.close()
l = MyStreamListener()
stream = tweepy.Stream(auth,l)
stream.filter(track=[])
虽然此代码没有错误,但程序不会像行中的投影输出那样打印任何内容
print("Tweet: " + tweet)
print("STATUS: " + status.text)
我已尝试调整线路
stream.filter(track=[])
包括
stream.filter(track=['trump','clinton'])
但我仍然没有收到任何输出。
请提供有关此问题发生原因或可能解决方案的帮助。
编辑:未来的调试表明 on_status 函数在任何时候都没有运行,尽管 tweepy docs. 中显示了这一点
【问题讨论】: