【发布时间】:2017-08-29 22:27:13
【问题描述】:
我目前正在编写用于流式传输 Twitter 帖子并将它们保存到 json 文件的代码。同时,textblob 确定推文的情绪。 到目前为止一切正常,但没有将所有输出保存到文件中。它目前保存推文,但不保存由 textblob 计算的情绪分数。这是我在 Python 中编码的第一天,我感谢每一点帮助:)
import textblob as textblob
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
from textblob import TextBlob
# consumer key, consumer secret, access token, access secret.
consumer_key = x
consumer_secret = x
access_token = x
access_token_secret = x
class StdOutlistener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
tweet = TextBlob(all_data["text"])
print(tweet)
print(tweet.sentiment)
# Open json text file to save the tweets
With open('tweets.json', 'a') as tf:
tf.write(data)
return True
def on_error(self, status):
print(status)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterStream = Stream(auth, StdOutlistener())
twitterStream.filter(languages=["en"], track=["Test"])
【问题讨论】:
-
您的问题到底是什么?
-
哦,我知道我有点不清楚:1:我想将推文与情绪结合起来。 2:我想知道如何将推文和情绪写入 json 文件。杰克做了一些假设,他们是正确的:)
标签: python twitter save streaming