-1
也許你可以幫助我。下面的python代碼檢索Twitter Streaming數據,並在獲得1000條tweet數據時停止。它可以工作,但會返回由選項卡分隔的字段「created_at,screen_name和text」。相反,我想以JSON格式獲取數據。我如何設置代碼才能獲取JSON格式的數據?Twitter流格式化JSON輸出
# Import the necessary package to process data in JSON format
try:
import json
except ImportError:
import simplejson as json
# Import the necessary methods from "twitter" library
from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
# Variables that contains the user credentials to access Twitter API
CONSUMER_KEY = '7pWHWtYlXM9ayJfUKv2F8v84B'
CONSUMER_SECRET = 'Dfcx10Px77Ggn0qGbCHc4TZC7M2IHsXpqk9CaGiCLzcr9VMX5n'
ACCESS_TOKEN = '245080367-zuLrIbxblOnocashgku9dsmDKgy3R7uU0VCTIRDx'
ACCESS_SECRET = 'wCx5ufD9Zft46hVjieLdv0af7p9DxUTsPgge9Zm2qelR9'
oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
# Initiate the connection to Twitter Streaming API
twitter_stream = TwitterStream(auth=oauth)
# Get a sample of the public data following through Twitter
#iterator = twitter_stream.statuses.sample()
iterator = twitter_stream.statuses.filter(track="Euro2016", language="fr")
tweet_count = 1000
for tweet in iterator:
tweet_count -= 1
print (tweet['created_at'],"\t",tweet['user']['screen_name'],"\t",tweet['geo'], "\t",tweet['text'])
if tweet_count <= 0:
break
*「它的工作原理,但返回的字段‘created_at,SCREEN_NAME和文本’由製表符分隔」 * - 和?這正是你所要求的。 – jonrsharpe
@jonrsharpe我想用JSON格式輸出數據 –
因此,改變代碼來做到這一點,但不要感到驚訝,當它確實寫入它的內容。你有問題嗎?也許讀[問]。 – jonrsharpe