如何从Tweepy获取Twitter用户名?

问题描述:

我一直在使用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:循环内执行此操作。

我认为你需要阅读来自Twitter Dev的文档以了解tweet的数据结构。

谢谢。

对于那些有兴趣,我想通了,在if decoded.get()循环中,我增加了以下内容:

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

然后打印行中我加了值:

print((decoded['coordinates'], user, date), file=text_file)