使用python获取推文的转推 [英] fetch the retweets for the tweets using python

查看:81
本文介绍了使用python获取推文的转推的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须获取推文的转推,并使用 python 脚本创建带有转推、用户 ID 等的 JSON 文件.请帮我解决我们的这个问题.

I have to fetch the retweets for the tweets and create the JSON file with retweets,user id etc using the python script. Kindly help me to sort it our this issues.

提前致谢!!

推荐答案

这个任务需要一些知识领域,既然你问的很笼统,我估计你需要一个脚本来立即运行,但设置这个过程需要某个时候

This task require some fields of knowledge, and since you ask in a general way, I reckon you need a script to run immediately, but setting up this process requires sometime

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

Use Twitter API call from Twython,

you can find a list here https://twython.readthedocs.io/en/latest/api.html, the param is the same as twitter API

response = twitter.get_retweets(id, 100)

分页

每次调用 API 都有返回限制,例如 engine.get_friends_ids 被限制为 5000 (https://dev.twitter.com/rest/reference/get/friends/ids),如果你想得到超过5000,你必须在返回的结果中使用游标(如果返回 json 中的 cur = 0 意味着没有更多结果),以下是如何处理游标的示例

Pagnation

each call to API have limit of returns, in example for engine.get_friends_ids was limited to 5000 (https://dev.twitter.com/rest/reference/get/friends/ids), if you want to get more than 5000, you have to use the cursor in the returned result (if cur = 0 in json returned means no more results), following is example of how to handling cursor

#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 密钥

密钥在一些调用后过期(https://dev.twitter.com/rest/public/rate-limits),因此您需要设置一些代码来自动更改您的密钥,或者等待一段时间(密钥达到限制返回错误代码 429)

API key

Key expires after some calls (https://dev.twitter.com/rest/public/rate-limits), so you need to set some code to auto change your key, or wait for some period (key reached limit return error code 429)

API 的响应是 JSON 格式,使用方便,可以通过选择 base on response[key] 来访问数据,例如响应["ids"] 或响应["next_cursor"]

The response from API was in JSON format, which was easy to use, you can access data by selecting base on response[key], in example reponse["ids"] or response["next_cursor"]

这篇关于使用python获取推文的转推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆