Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Using Twitter API to grab followers from specific user i'ds?

Hello,

I am trying to grab followers from specific users using the GET followers/ids from Twitters REST API. I can get the code to execute, returns the following error message

NameError: name 'get_followers_ids' is not defined

My code is below, thank you for your help:

#!GET followers/ids 

import logging
import time
import csv
import twython
import json

app_key = "" #REDACTED
app_secret = "" #REDACTED
oauth_token = "" #REDACTED
oauth_token_secret = "" #REDACTED


get_followers_ids('Cycled_md')

outfn = "twitter_user_data_%i.%i.%i.txt" % (now.month, now.day, now.year)

twitter = twython.Twython(app_key, app_secret, oauth_token, oauth_token_secret)  

followers = twitter.get_followers_ids(screen_name = "Cylced_md")  

with open('cycledfollowers.txt', 'a') as outfile:  
    for follower_id in followers:
        print("Cycled_md follower:", follower_id)
        json.dump(follower_id, outfile, sort_keys = True, indent = 4)

I'm updating your post to remove your API keys. In the future, don't EVER include any of your keys, it essentially a password and other can use those credentials on your behalf.

Thank you Kai,

That was careless of me. I did not even pay attention to that when I initially posted this. Much appreciated!

1 Answer

Max Hirsh
Max Hirsh
16,773 Points

I'm not familiar with Twitter's API, but would this be what you are looking for? https://dev.twitter.com/rest/reference/get/followers/ids