8

I was just wondering if anyone knew how to list out the usernames that a twitter user is following, and their followers in two separate .csv cells. This is what I have tried so far.

import tweepy
import csv

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

csvFile = open('ID.csv', 'w')
csvWriter = csv.writer(csvFile)

users = ['AindriasMoynih1', 'Fiona_Kildare', 'daracalleary', 'CowenBarry', 'BillyKelleherTD', 'BrendanSmithTD']


for user_name in users:
    user = api.get_user(screen_name = user_name, count=200)
    csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.followers_id, user.friends_id user.description.encode('utf-8')])
    print (user.id)


csvFile.close()
  • What is your output when running the program? – T.Woody Sep 21 '18 at 20:22
  • I get an error "'User' object has no attribute 'followers_id'" @T.Woody –  Sep 21 '18 at 20:23
  • Try a CRUD approach, and do `for row in user: print(row)`. From there, do `pragma table_info(myTable)` and see what the column names are. I am guessing it is a typo or the column name does not exist. – T.Woody Sep 21 '18 at 20:27
  • And please be a thoughful person, by coming back here and updating us on the findings. – T.Woody Sep 21 '18 at 20:34

1 Answers1

16

Tweepy is a wrapper around the Twitter API.

According to the Twitter API documentation, you'll need to call the GET friends/ids to get a list of their friends (people they follow), and GET followers/ids to get their followers.

Using the wrapper, you'll invoke those API calls indirectly by calling the corresponding method in Tweepy.

Since there will be a lot of results, you should use the Tweepy Cursor to handle scrolling through the pages of results for you.

Try the code below. I'll leave it to you to handle the CSV aspect, and to apply it to multiple users.

import tweepy

access_token = "1234"
access_token_secret = "1234"
consumer_key = "1234"
consumer_secret = "1234"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

for user in tweepy.Cursor(api.get_friends, screen_name="TechCrunch").items():
    print('friend: ' + user.screen_name)

for user in tweepy.Cursor(api.get_followers, screen_name="TechCrunch").items():
    print('follower: ' + user.screen_name)
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
jschnurr
  • 1,181
  • 6
  • 8
  • That's perfect, thank you! Although I am getting a rate limit exceeded error. How would I be able to get all of the users without getting this error? –  Sep 22 '18 at 13:11
  • 5
    Try `api = tweepy.API(auth, wait_on_rate_limit=True)` ; that will tell Tweepy to pause until the rate limit expires, instead of giving an error. – jschnurr Sep 22 '18 at 13:21
  • 1
    Tweepy now has ```api.get_friends``` instead of ```api.friends``` . Source: https://docs.tweepy.org/en/stable/api.html#tweepy.API.get_friends – plutownium May 03 '22 at 04:20