如何从 Tweepy 获取 Twitter 用户名?

How to get Twitter username from Tweepy?

我一直在使用 Tweepy 通过流 API 收集某个区域的推文,我只收集 latitude/longitude 的推文,但我想添加更多推文我不确定具体是什么。我正在使用这段代码来获取 lat/long 值:

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)