Skip to content Skip to sidebar Skip to footer

Fetch The Retweets For The Tweets Using Python

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. Thanks in advance

Solution 1:

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

This part to get connect to twitter API

from twython import Twython, TwythonErrorAPP_KEY='YOUR_APP_KEY'
APP_SECRET = 'YOUR_APP_SECRET'

twitter = Twython(APP_KEY, APP_SECRET)

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)

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 toloop    
cur = -1

#Stopwhen 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

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)

Response

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"]

Post a Comment for "Fetch The Retweets For The Tweets Using Python"