2017-09-26 159 views
0

我一直在使用Tweepy通過流媒體API收集某個區域的Tweets,並且我一直只收集推文的緯度/經度,但我想添加更多內容,但我不確定具體內容是。我使用的代碼塊得到的緯度/經度值:如何從Tweepy獲取Twitter用戶名?

import json, tweepy 
from html.parser import HTMLParser 

consumer_key = "" 
consumer_secret = "" 
access_token = "" 
access_secret = "" 

count = 0 

class StdOutListener(tweepy.StreamListener): 
    def on_data(self, data): 
     global count 
     decoded = json.loads(HTMLParser().unescape(data)) 
     if decoded.get('coordinates',None) is not None: 
     coordinates = decoded.get('coordinates','').get('coordinates','') 
     name = decoded.get('name','') 
     with open("C:\\Users\\gchre\\Desktop\\Tweets.txt", "a") as text_file: 
      print(decoded['coordinates'], file=text_file) 
     print(decoded['coordinates']) 
     count += 1 
     return True 
    def on_error(self, status): 
     print(status) 

l = StdOutListener() 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_secret) 
stream = tweepy.Stream(auth, l) 

while count < 1000000: 
    stream.filter(locations=[-88.853859,41.220047,-86.953073,42.758134]) 

我想這還輸出到文本文件中的特定的用戶名(@Handle)和鳴叫的創建時間。我不確定我是否應該在if decoded.get('coordinates',None) is not None:循環內執行此操作。

回答

1

我認爲你需要閱讀來自Twitter Dev的文檔以瞭解tweet的數據結構。

謝謝。

1

對於那些有興趣,我想通了,在if decoded.get()循環中,我增加了以下內容:

user = decoded.get('user','').get('screen_name','') 
date = decoded.get('created_at','') 

然後打印行中我加了值:

print((decoded['coordinates'], user, date), file=text_file) 
相關問題