2016-07-10 53 views
-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 
+0

*「它的工作原理,但返回的字段‘created_at,SCREEN_NAME和文本’由製表符分隔」 * - 和?這正是你所要求的。 – jonrsharpe

+0

@jonrsharpe我想用JSON格式輸出數據 –

+0

因此,改變代碼來做到這一點,但不要感到驚訝,當它確實寫入它的內容。你有問題嗎?也許讀[問]。 – jonrsharpe

回答

1

您可以導入tweepy(您需要使用PIP先安裝),並覆蓋listener class能夠輸出JSON格式的數據。這裏有一個例子:

from tweepy import Stream 
from tweepy.streaming import StreamListener 

#Listener Class Override 
class listener(StreamListener): 
    def on_data(self, data): 
     try: 
      tweet = json.loads(data) 
      with open('your_data.json', 'a') as my_file: 
       json.dump(tweet, my_file) 
     except BaseException: 
      print('Error') 
      pass 

    def on_error(self, status): 
     print(statuses) 

my_listener=listener() 
twitterStream = Stream(oauth, my_listener) #Inizialize Stream object 

你可以閱讀更多關於tweepy這裏:http://docs.tweepy.org/en/v3.4.0/streaming_how_to.html