How to Fetch Data From Instagram Using Python

Instagram is one of the most popular social media sites with billions of users. Everyone from college students to celebrities has Instagram accounts. Instagram’s public data can be of immense value to businesses, marketers, and individuals. Anyone can use this data to perform data analysis, conduct targeted marketing, and generate insights.

You can use Python to create an automated tool that extracts Instagram data.

Install required libraries

Instalader is a Python library that you can use to extract publicly available data from Instagram. You can access data like pictures, videos, username, no. of Posts, Follower Count, Follower Count, Bio etc. with Instalader. Note that Instalader is in no way affiliated with, authorized, maintained or endorsed by Instagram.

Run the following command to install the instalader via pip:

pip install instaloader

You must have pip installed on your system to install external Python libraries.

Next you need to install the pandas python library. Pandas is a Python library mainly used for data manipulation and data analysis. Run the following command to install it:

pip install pandas

Now you can start setting up the code and getting the data from Instagram.

Set up your code

To set up the Instagram data retrieval tool, you need to import the Instalader python library and create an instance of the Instalader class. After that, you need to provide the Instagram handle of the profile from which you want to extract the data.

Instagram Extractor’s Python code is available in a GitHub repository and is free for you to use under the MIT license.

import instaloader

bot = instaloader.Instaloader()

profile = instaloader.Profile.from_username(bot.context, 'cristiano')

This is a good first step to review the base work. You should see some meaningful data with no errors:

You can upload valuable publicly available data like Username, Post #, Follower Count, Follower Count, Bio, User ID and External URL with Instalader with just a few lines of code. You just need to provide the Instagram handle of the profile.

import instaloader
import pandas as pd

bot = instaloader.Instaloader()

profile = instaloader.Profile.from_username(bot.context, 'leomessi')
print("Username: ", profile.username)
print("User ID: ", profile.userid)
print("Number of Posts: ", profile.mediacount)
print("Followers Count: ", profile.followers)
print("Following Count: ", profile.followees)
print("Bio: ", profile.biography)
print("External URL: ", profile.external_url)

You should see lots of profile information from the handle you provided:

You can extract email addresses from any profile’s Insta-Bio using regular expressions. You need to import the pythons concerning Library and pass the regular expression to validate the email as a parameter to the re.findall() Method:

import instaloader
import re
bot = instaloader.Instaloader()
profile = instaloader.Profile.from_username(bot.context, "wealth")
print("Username: ", profile.username)
print("Bio: ", profile.biography)
emails = re.findall(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", profile.biography)
print("Emails extracted from the bio:")

The script will print whatever it recognizes as an email address in the bio:

If you search for anything on Instagram, you’ll get multiple results, including usernames and hashtags. You can extract the top search results with get_profiles() and get_hashtags() methods. You only have to specify the search query in the installoader.TopSearchResults() Method. You can also iterate over and print/save the individual results.

import instaloader

bot = instaloader.Instaloader()

search_results = instaloader.TopSearchResults(bot.context, 'music')

for username in search_results.get_profiles():

for hashtag in search_results.get_hashtags():

The output includes all matching usernames and hashtags:

You can extract an account’s followers and those it follows itself with Instalader. You must provide an Instagram username and password to retrieve this data.

Never use your personal accounts to extract data from Instagram as it may result in temporary or permanent ban of your account.

After creating an instance of the Instalader class, you will need to provide your username and password. This will allow the bot to login to Instagram with your account and get the followers and followers data.

Next, you need to provide the Instagram handle of the target profile. That get_followers() and get_followees() Methods extract the followers and followees. You can get the usernames of followers and followers using follower.username and followee.username properties or

If you want to save the results to a CSV file, you must first convert the data to a Pandas DataFrame object. Use the pd.DataFrame() method to convert a list object to a DataFrame.

Finally, you can export the DataFrame object to a CSV file using to_csv() Method. You have to pass the filename.csv as a parameter for this method to get the exported data in CSV file format.

Only the account owners can see all followers and followers. You cannot extract all followers and followers data using this method or any other.

import instaloader
import pandas as pd

bot = instaloader.Instaloader()
bot.login(user="Your_username", passwd="Your_password")

profile = instaloader.Profile.from_username(bot.context, 'Your_target_account_insta_handle')

followers = [follower.username for follower in profile.get_followers()]

followers_df = pd.DataFrame(followers)

followers_df.to_csv('followers.csv', index=False)

followings = [followee.username for followee in profile.get_followees()]

followings_df = pd.DataFrame(followings)

followings_df.to_csv('followings.csv', index=False)

Download posts from an Instagram account

Again, downloading posts from any account requires you to provide a username and password. This allows the bot to log into Instagram with your account. You can call up all the data of the posts with get_posts() Method. And you can iterate and download all the individual posts with download_post() Method.

import instaloader
import pandas as pd

bot = instaloader.Instaloader()

profile = instaloader.Profile.from_username(bot.context, 'Your_target_account_insta_handle')

posts = profile.get_posts()

for index, post in enumerate(posts, 1):
bot.download_post(post, target=f"{profile.username}_{index}")

Scrape the web with Python

Data scraping or web scraping is one of the most common ways to extract useful information from the internet. You can use the extracted data for marketing, content creation or decision making.

Python is the language of choice for data scraping. Libraries like BeautifulSoup, Scrapy, and Pandas simplify data extraction, analysis, and visualization.

Leave a Reply

Your email address will not be published. Required fields are marked *