【问题标题】:How to get Twitter username from Tweepy?如何从 Tweepy 获取 Twitter 用户名?
【发布时间】:2018-03-07 22:29:59
【问题描述】:

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

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: 循环中执行此操作。

【问题讨论】:

    标签: python python-3.x twitter tweepy


    【解决方案1】:

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

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

    然后在打印行中我添加了值:

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

    【讨论】:

      【解决方案2】:

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

      谢谢。

      【讨论】:

        猜你喜欢
        • 2020-06-05
        • 1970-01-01
        • 2016-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-07
        相关资源
        最近更新 更多