Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Last active May 3, 2022 05:48
Show Gist options
  • Save PandaWhoCodes/46f58fdead71f4c71453d9ed1e21adf8 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/46f58fdead71f4c71453d9ed1e21adf8 to your computer and use it in GitHub Desktop.
best way get all twitter followers of a user using tweepy
import time
import tweepy
import csv
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("",
"")
def get_followers(user_name):
"""
get a list of all followers of a twitter account
:param user_name: twitter username without '@' symbol
:return: list of usernames without '@' symbol
"""
api = tweepy.API(auth)
followers = []
for page in tweepy.Cursor(api.followers, screen_name=user_name, wait_on_rate_limit=True,count=200).pages():
try:
followers.extend(page)
except tweepy.TweepError as e:
print("Going to sleep:", e)
time.sleep(60)
return followers
def save_followers_to_csv(user_name, data):
"""
saves json data to csv
:param data: data recieved from twitter
:return: None
"""
HEADERS = ["name", "screen_name", "description", "followers_count", "followers_count",
'friends_count', "listed_count", "favourites_count", "created_at"]
with open(user_name + "_followers.csv", 'w',encoding="utf-8") as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(HEADERS)
for profile_data in data:
profile = []
for header in HEADERS:
profile.append(profile_data._json[header])
csv_writer.writerow(profile)
if __name__ == '__main__':
followers = get_followers("ashish_che")
save_followers_to_csv("ashish_che", followers)
@michaelGRU
Copy link

That makes sense! Thank you for the comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment