我目前正在處理一個代碼,以流twitter的帖子,並將它們保存到json文件。同時,textblob決定推文的情緒。 目前爲止所有工作都正常,但並未將所有輸出保存到文件中。它目前保存推文,但不保存textblob計算的情感分數。這是在Python我的第一天編碼和我欣賞的幫助每一位:)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"])
到底什麼是你的問題? –
O我看到我有點不清楚:1:我想結合tweets和情緒。 2:我想知道如何將推文和情緒寫入json文件。傑克做了一些假設,他們是正確的:) – Robbert