How to download twitter friends or followers for free

How to download twitter friends or followers for free

Play all audios:

Loading...

A SIMPLE GUIDE IN PYTHON When you Google how to do this, all these companies pop up, offering to do this for you if you pay them a small fee. But Twitter gives you all the tools you need to


do it yourself—for free. This short guide takes you through a Python script that helps you to use those tools. At the end of this guide, you get a script that downloads a CSV containing all


the followers and/or friends for any public Twitter handle. This file will also contain the following details about each person: * username * user id * handle * location * bio * url *


followers count * friends count * date account was created * verified status Sample CSV file for the followers of @briennakh: TABLE OF CONTENTS: * Set up your Twitter developer account *


Generate authentication tokens * Write the Python script * Run the script 1. SET UP YOUR TWITTER DEVELOPER ACCOUNT To use the Twitter API to download bulk data such as friends and/or


followers, you need a developer’s account, a project, and an app. If you already have all these, you can skip this step. If you don’t already have a Developer’s account, get one here:


Helpful documentation: * Developer Portal * Project * App It may take a while to get approved, up to a few days or weeks. If you don’t have the time to wait for approval, I would be happy to


quickly get your data for you. You can find me on UpWork: 2. GENERATE AUTHENTICATION TOKENS Once you have a developer’s account, you need to generate the authentication tokens that would


allow you to connect to the API from a script. In your Developer Portal dashboard, click on the key icon next to your app: It will take you to your app page. Here, you need to generate or


regenerate four pieces of information: * consumer API key * consumer API secret * access token * access secret Immediately save these tokens to a file named CONFIG.INI, since once you finish


viewing any of these, you cannot view them again. You will have to regenerate them. Your config.ini must look like this: [TWITTER] CONSUMER_KEY = YOUR_CONSUMER_KEY_HERE CONSUMER_SECRET = 


YOUR_CONSUMER_API_SECRET_HERE ACCESS_TOKEN = YOUR_ACCESS_TOKEN_HERE ACCESS_SECRET = YOUR_ACCESS_SECRET_HERE Swap out all the instances of YOUR_ETC_HERE with your tokens. Do not leave any


blank spaces between the beginning of the file and [TWITTER]. I often use config.ini files to avoid directly pasting authentication tokens into my script, which makes it safe to make the


script public, as my authentication tokens are now hidden. 3. WRITE THE PYTHON SCRIPT To make it easier to connect to the Twitter API, we will use the Tweepy package. With this package, our


script involves four steps: * Authenticate our connection. * Get the Twitter id for each follower or friend. * Get more details for each follower or friend, such as their handle, username,


and bio. The full list can be found at the beginning of this guide. * Process the data into a usable format. We’ll save this script as a file named DOWNLOAD_FRIENDS_OR_FOLLOWERS.PY and save


it to the same directory as our CONFIG.INI file. DIRECTORY STRUCTURE: twitter-helper-scripts  |_ config.ini  |_ download_friends_or_followers.py AUTHENTICATE OUR CONNECTION We open


configs.ini and read in the tokens, and then use them to authenticate our connection to the Twitter API. import configparser from tweepy import API, Cursor, OAuthHandler, TweepError# Read in


 configs configs = configparser.ConfigParser() configs.read('./config.ini') keys = configs['TWITTER'] consumer_key = keys['CONSUMER_KEY']  consumer_secret = 


keys['CONSUMER_SECRET']  access_token = keys['ACCESS_TOKEN'] access_secret = keys['ACCESS_SECRET']# Authenticate Tweepy connection to Twitter API auth = 


OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) GET THE TWITTER ID FOR


EACH FOLLOWER OR FRIEND To get the id for each follower of a given handle: screen_name = 'briennakh' ids = [] for fid in Cursor(api.followers_ids, screen_name=screen_name, 


count=5000).items():     ids.append(fid) The process is identical for friends, except that we change the API endpoint from api.follower_ids to api.friend_ids: screen_name = 


'briennakh' ids = [] for fid in Cursor(api.friends_ids, screen_name=screen_name, count=5000).items():     ids.append(fid) Now we have ids, which is a list of follower ids, or a


list of friend ids, depending on the API endpoint we used. GET MORE DETAILS FOR EACH FOLLOWER OR FRIEND A list of ids is almost useless, except for requesting more data that we can actually


use. So we submit these ids to api.lookup_users, the API endpoint responsible for returning details about individual users. info = [] for i in range(0, len(ids), 100):     try:         chunk


 = ids[i:i+100]         info.extend(api.lookup_users(user_ids=chunk))     except:         import traceback         traceback.print_exc()         print('Something went wrong, 


skipping...') Here, we broke down ids into chunks of 100 ids, which is the maximum chunk size that this API endpoint accepts per request. We save the returned user details to info,


extending this list every time a chunk finishes processing. In the end, info will contain one record for each follower or friend, which looks like this: PROCESS THE DATA INTO A USABLE FORMAT


These records are not easily usable. A different format such as CSV would be much more easily usable later on, so we process these records and download them as a CSV file. import pandas as 


pddata = [x._json for x in info] df = pd.DataFrame(data) df = df[['id', 'name', 'screen_name', 'location', 'description', 'url', 


'followers_count', 'friends_count', 'created_at', 'verified']] df.to_csv('followers.csv', index=False) Note that I did not keep all possible


details about each follower or friend. If you see a detail in the record that you want to add to your CSV, you can edit the code to include it. Likewise, you can also drop details that you


are not interested in saving. Furthermore, keep in mind that if you are friends with someone, and they also follow you, then this person will show up in both the followers list and the


friends list. Our script does not deal with this type of post-processing. 4. RUN THE SCRIPT You can copy and paste the above code into your own script, or you can find an executable version


of the script here: To use this script, * Download two files: the SCRIPT and its REQUIREMENTS.TXT. Place both files into the same directory, along with your CONFIG.ini from above. You should


have 3 files in total in this directory. * Using the command line, navigate to this directory. Download the Python requirements: pip install -r requirements.txt * Run the script: python


download_friends_or_followers.py -u [user] -t [type] For the [user] argument, enter the Twitter handle for which you want to get the followers and/or friends. This argument is required. For


the [type] argument, enter the type you want: friends, followers, or both. This argument is not required. The default is followers. Example: python download_friends_or_followers.py -u


briennakh -t both You should now see one or two CSV files appear in your directory, depending on the type you requested. IF YOU’D LIKE TO READ MORE OF MY ARTICLES OR EXPLORE MILLIONS OF


OTHER ARTICLES, YOU CAN SIGN UP FOR MEDIUM MEMBERSHIP: JOIN MEDIUM WITH MY REFERRAL LINK — BRIENNA HEROLD AS A MEDIUM MEMBER, A PORTION OF YOUR MEMBERSHIP FEE GOES TO WRITERS YOU READ, AND


YOU GET FULL ACCESS TO EVERY STORY… brienna.medium.com YOU CAN ALSO SUBSCRIBE TO MY EMAIL LIST TO GET NOTIFIED WHENEVER I PUBLISH A NEW ARTICLE: GET AN EMAIL WHENEVER BRIENNA HEROLD


PUBLISHES. GET AN EMAIL WHENEVER BRIENNA HEROLD PUBLISHES. BY SIGNING UP, YOU WILL CREATE A MEDIUM ACCOUNT IF YOU DON’T ALREADY… brienna.medium.com SOME OTHER STORIES FROM ME THAT MIGHT


INTEREST YOU: HOW TO BULK ACCESS ARXIV FULL-TEXT PREPRINTS WITH PYTHON3 AND THE MACOS X COMMAND LINE towardsdatascience.com COLLECTING DATA FROM THE NEW YORK TIMES OVER ANY PERIOD OF TIME A


SIMPLE GUIDE IN PYTHON towardsdatascience.com HOW TO DETECT CODE PLAGIARISM A SHORT TUTORIAL ON USING STANFORD’S MOSS medium.com