Python 运行StreamListener(Tweepy)并同时分析推文
我目前正在使用Tweepy来传输推文,并将每条推文输出到json文件中。一旦我听完并关闭了流,我就开始分析推特的情绪。我想知道是否有一种方法可以同时做到这一点。我想启动流,在json文件中输出tweet,然后在该tweet上运行我的情绪分析,然后对每个tweet进行一次又一次的实时分析Python 运行StreamListener(Tweepy)并同时分析推文,python,twitter,stream,tweepy,Python,Twitter,Stream,Tweepy,我目前正在使用Tweepy来传输推文,并将每条推文输出到json文件中。一旦我听完并关闭了流,我就开始分析推特的情绪。我想知道是否有一种方法可以同时做到这一点。我想启动流,在json文件中输出tweet,然后在该tweet上运行我的情绪分析,然后对每个tweet进行一次又一次的实时分析 def on_status(self, status): self.output.write(status + "\n") self.counter += 1 if self.coun
def on_status(self, status):
self.output.write(status + "\n")
self.counter += 1
if self.counter >= 20000:
self.output.close()
self.output = open('../streaming_data/' + self.fprefix + '.' + time.strftime('%Y%m%d-%H%M%S') + '.json', 'w')
上面是我的流侦听器。输出文件为self.output
tweets = {}
with open(output.json, 'r') as file:
lines = (line.rstrip() for line in file)
lines = (line for line in lines if line)
for line in lines:
tweet = json.loads(line)
tweets[tweet['id']]= tweet
以上是我如何在tweets中存储每条tweet,以便使用函数分析它们。我的函数将tweets作为参数
function = myFunction(tweets, pos, neg)
实际上,StreamListener收集推文并将其存储在json文件中。但我想收集推文,一收到就分析。因此,收集一条tweet,然后分析它,然后再做一次。是否有任何具体原因说明为什么要将tweet存储为json文件?为什么不简单地将tweet文本存储为文本文件,因为我假设您只对tweet文本进行分析。另外,为什么不调用一个函数来处理tweet并让它同时写入文件 可能看起来如下所示:
import tweepy
import secrets
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
process_tweet(status.text)
def process_tweet(tweet):
with open('tweets.txt', 'w') as tweet_file:
tweet_file.write(tweet)
sentiment_analysis(tweet)
def sentiment_analysis(tweet):
#code to determine the sentiment of a tweet
您应该使用async参数在另一个线程中执行流:
aStream.filter(track=[aFilter], async=True)
或
aStream.userstream(async=True)