我必须获取推文的转发,并使用python脚本创建带有转发、用户id等的JSON文件。请帮我解决这个问题。
提前感谢!!
发布于 2017-05-03 19:04:01
这项任务需要一些知识领域,而且由于您一般地询问,我认为您需要一个脚本来立即运行,但设置此过程需要一些时间
此部分用于连接到twitter API
from twython import Twython, TwythonError
APP_KEY = 'YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'
twitter = Twython(APP_KEY, APP_SECRET)
使用来自Twython的Twitter API调用,
你可以在这里找到一个列表https://twython.readthedocs.io/en/latest/api.html,参数与twitter API相同
response = twitter.get_retweets(id, 100)
Pagnation
每次调用API都有返回限制,例如engine.get_friends_ids被限制为5000 (https://dev.twitter.com/rest/reference/get/friends/ids),如果你想获得5000以上的结果,则必须在返回结果中使用游标(如果json returns中的cur =0表示不再返回结果),下面是处理游标的示例
#Set a temp to loop
cur = -1
#Stop when no more result
while cur !=0:
response = twitter.get_friends_ids(user_id=user_id, cursor=cur)
#Some code to handle the response
cur = response["next_cursor"]
API密钥
Key在一些调用(https://dev.twitter.com/rest/public/rate-limits)后过期,因此您需要设置一些代码来自动更换key,或者等待一段时间(key reached limit返回错误代码429)
响应
来自API的响应是JSON格式的,这很容易使用,您可以通过选择基于响应键来访问数据,例如响应“ids”或响应“next_cursor”。
https://stackoverflow.com/questions/43741339
复制相似问题