簡體   English   中英

通過Twython獲取推文ID?

[英]Getting a tweet's ID with Twython?

我正在使用Twython(Twitter API的Python包裝器,請參見此處 。)

目標:我正在嘗試創建一個簡單的機器人來搜索關鍵字,並使用其中的關鍵字回復推文。

示例:發送搜索請求以搜索#stackoverflow ,並使用“ StackOverflow最好!”回復其中包含#stackoverflow推文。

問題:沒有tweet ID(可在任何永久鏈接的tweet的url中找到),無法回復該tweet。 例如,獲取任何推文並將某人鏈接到該推文。 鏈接末尾的數字是tweet ID。

我嘗試過的東西:我沒有什么可以嘗試的。 我希望它盡可能簡單,沒有復雜的解決方法。 我相信有某種方法可以做到這一點,而不必太過分。 我已經用盡了Google和Twython的文檔以及Twitter的API文檔。 = /任何人

Tweet只是python詞典,它們的內容恰好呼應Tweet資源 因此,每個推文都有一個id_str密鑰:

print tweet['id_str']

如果情況不清楚,您始終可以打印數據結構。 我可以推薦pprint.pprint()函數使嵌套的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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM