【问题标题】:Getting a tweet's ID with Twython?使用 Twython 获取推文 ID?
【发布时间】:2012-09-18 05:04:45
【问题描述】:

我正在使用 Twython(Twitter API 的 Python 包装器,找到 here。)

目标:我正在尝试制作一个简单的机器人来搜索关键字并回复带有关键字的推文。

示例:发送搜索请求以搜索#stackoverflow,回复包含#stackoverflow 的推文并带有“StackOverflow 是最好的!”

问题:无法回复没有推文 ID 的推文(在任何永久链接推文的 url 中找到)。这方面的一个例子是获取任何推文并将某人链接到它。链接末尾的数字是推文 ID。

我尝试过的:我可以尝试的不多。我希望这尽可能简单,没有复杂的解决方法。我确信有一些方法可以做到这一点,而不必走得太远。我已经用尽了 Google 和 Twython 的文档以及 Twitter 的 API 文档。 =/ 任何人

【问题讨论】:

    标签: python api twitter twython


    【解决方案1】:

    推文只是 python 字典,它们的内容与 Tweet resource 完全一致。因此,每条推文都有一个id_str 键:

    print tweet['id_str']
    

    如果不清楚,您可以随时打印数据结构;我可以推荐 pprint.pprint() function 以使嵌套的 Python 结构更具可读性:

    import pprint
    
    pprint.pprint(tweet)
    

    示例会话:

    >>> from twython import Twython
    >>> t = Twython()
    >>> res = t.search(q='python')
    >>> res.keys()
    [u'next_page', u'completed_in', u'max_id_str', u'since_id_str', u'refresh_url', u'results', u'since_id', u'results_per_page', u'query', u'max_id', u'page']
    >>> from pprint import pprint
    >>> pprint(res[u'results'][0])
    {u'created_at': u'Mon, 17 Sep 2012 21:01:12 +0000',
     u'from_user': u'Me_Craay_GOOFY',
     u'from_user_id': 230100184,
     u'from_user_id_str': u'230100184',
     u'from_user_name': u'\u06deSuperFLY_PUER\u06de\u2122',
     u'geo': None,
     u'id': 247802407529115649,
     u'id_str': u'247802407529115649',
     u'iso_language_code': u'en',
     u'metadata': {u'result_type': u'recent'},
     u'profile_image_url': u'http://a0.twimg.com/profile_images/2617747450/345616051_normal.jpg',
     u'profile_image_url_https': u'https://si0.twimg.com/profile_images/2617747450/345616051_normal.jpg',
     u'source': u'<a href="http://globalgrind.com">UncleUber for Blackberry</a>',
     u'text': u'RT @Mr_Oyato: #ViolentPrayers May the python of breakthrough swallow you and your household today.',
     u'to_user': None,
     u'to_user_id': 0,
     u'to_user_id_str': u'0',
     u'to_user_name': None}
    >>> res[u'results'][0]['id_str']
    u'247802407529115649'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      相关资源
      最近更新 更多