【问题标题】:Parsing Twitter json with Python用 Python 解析 Twitter json
【发布时间】:2015-07-22 09:35:38
【问题描述】:

我在 python 中使用 twython 库来转储我自己的公共推文。数据以 json 格式下载,参考: https://api.twitter.com/1.1/statuses/home_timeline.json

如何逐行打印所有数据,比如

    print "Tweet : %s" %tweet['text']#status
    print "Create Time : %s" %tweet['created_at']#time of tweet
    print "Geo location : %s" %tweet['geo']#geo location if avail
    print "Favorite Count : %s" %tweet['favorite_count']
    print "Source : %s" %tweet["source"]
    print "Retweeted : %s" %tweet["retweeted"]
    print "contributors :%s" %tweet["contributors"]
    print "truncated : %s" %tweet["truncated"]
    print "is_quote_status : %s" %tweet["is_quote_status"]
    print "in_reply_to_status_id : %s" %tweet["in_reply_to_status_id"]
    print "Unique ID : %s" %tweet["id"]
    print "coordinates : %s" %tweet["coordinates"]
    print "in_reply_to_screen_name : %s" %tweet["in_reply_to_screen_name"]
    print "retweet_count : %s" %tweet["retweet_count"]
    print "in_reply_to_user_id : %s" %tweet["in_reply_to_user_id"]
    print "favorited :%s" %tweet["favorited"]

【问题讨论】:

  • 如果你有json,你可以使用json.loads将它转换成pythondict
  • 不要把事情复杂化,你可以使用Tweepy模块来访问推文。
  • 任何示例代码开始。 json输出的格式将在链接api.twitter.com/1.1/statuses/home_timeline.json
  • @zdaR tweepy,像 twython 一样简单吗?您想指出任何优点和缺点吗?没有多少方法可用于用户时间轴tweepy.readthedocs.org/en/v3.2.0/api.html#API.user_timeline
  • 也许你可以考虑使用nltk。有一些新的功能可以读取 twitter jsons,虽然还没有发布。请查看此notebookExtracting Parts of a Tweet 段。如果您想使用它,您必须下载项目源并将其添加到您的 PYTHONPATH。不知道什么时候真正发布。 (顺便说一句,这是使用twython 访问推特。希望对您有所帮助。

标签: python twitter twython


【解决方案1】:

考虑到您使用 twython 获得了 json 格式的推文,它看起来像:- "{'text' : 'abc', 'created_at': '<created_date>'}"

您可以使用python json 喜欢:-

>>import json
>>tweet_json = <your_json>
>>python_datastruct = json.loads(tweet_json)

上面的示例将返回一个 python 数据结构,您可以使用它来打印所需的信息。

编辑: 对于嵌套对象,请尝试以下操作:-

global_dict = {'a':{'a1':{'a11':1, 'a12':2}, 'a2':3}, 'b':4}
def print_recur(py_item):
    for key, value in py_item.items():
        print key
        if type(value) == dict:
            print_recur(value)
        else:
            print value

print_recur(global_dict)

这将遍历您的嵌套字典以打印所有键和值。

【讨论】:

  • 如果值嵌套到另一个值中会发生什么?如何自动print嵌套值?可能样本数据 {u'contributors': None, u'truncated': False, u'text': u'Dumping data for art。 #dataviz', u'is_quote_status': False, u'in_reply_to_status_id': 无, u'id': 623497430017839104, u'favorite_count': 0, u'source': u'twitter.com" rel= "nofollow">Twitter Web Client', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u 'hashtags': [{u'indices': [22, 30], u'text': u'dataviz'}], u'urls': []},继续...
  • 您可以尝试打印python_datastruct。这将转储所有数据。所以你会得到接收数据的结构。大多数情况下,所有收到的推文的结构都是相同的。因此,您可以使用通用函数来打印您的自定义数据。
  • 通用函数?喜欢。是不是和这个stackoverflow.com/questions/23306653/…类似
  • 请检查编辑....是的,我提到了您所指出的内容。但是让它更通用。 :)
  • 感谢您的帮助,但还有一个嵌套值被卡住了。
猜你喜欢
  • 2012-11-09
  • 2013-12-07
  • 2013-01-19
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 2019-08-11
相关资源
最近更新 更多