Last active
May 3, 2022 05:48
-
-
Save PandaWhoCodes/46f58fdead71f4c71453d9ed1e21adf8 to your computer and use it in GitHub Desktop.
best way get all twitter followers of a user using tweepy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
I keep getting this error: 'API' object has no attribute 'followers'. Any idea what might be the problem? Is this code outdated?
The code is more than 3 years old. twitter API's have updated.
Checkout - https://docs.tweepy.org/en/stable/api.html for the latest API ref
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
I keep getting this error: 'API' object has no attribute 'followers'. Any idea what might be the problem? Is this code outdated?