Scraping Instagram with Python

You must have seen many articles and tutorials on web scraping. Even I have written an article on it. In this article, I will do something different, that is scraping Instagram with Python. It is an easy task until you get errors in writing your usernames and passwords. Here I will show you some techniques that will help you in scraping Instagram with Python in a much easy way.

To scrap Instagram, we will use a library know as instaloader which provides us with an API for scraping Instagram. You can install this library by using the pip method in your terminal – pip install instaloader. Now If you have installed this package then let’s get started with the task.

Scraping Instagram with Python

First of all, if you are learning Data Science then scraping Instagram will help you in getting the new trends of businesses, so that you can generate more leads and can reach out for your new potential customers. Now let’s start with scraping Instagram users profiles:

# Import the module
!pip install instaloader
import instaloader

# Create an instance of Instaloader class
bot = instaloader.Instaloader()

# Load a profile from an Instagram handle
profile = instaloader.Profile.from_username(bot.context, 'aman.kharwal')

print(type(profile))Code language: Python (python)

<Profile aman.kharwal (1986707800)>

Now let’s see how we can extract some valuable information from an Instagram profile:

print("Username: ", profile.username)
print("User ID: ", profile.userid)
print("Number of Posts: ", profile.mediacount)
print("Followers: ", profile.followers)
print("Followees: ", profile.followees)
print("Bio: ", profile.biography,profile.external_url)Code language: Python (python)

Username: aman.kharwal User ID: 1986707800 Number of Posts: 105 Followers: 521 Followees: 154 Bio: Data Scientist Data Science | Machine Learning | AI | Python

Now let’s see how you can log in to your Instagram profile using python:

# Login with username and password in the script
bot.login(user="your username",passwd="your password")

# Interactive login on terminal
bot.interactive_login("your username") # Asks for password in the terminalCode language: Python (python)

You will think, why you logged in? Logging in was very important for a lot of methods that we are going to explore further.

Scraping Instagram Followers and Followees

Scraping your followers and followees will help you in getting a list of their usernames, which you will require to do when you will work in a professional environment in the data science field:

# Retrieve the usernames of all followers
followers = [follower.username for follower in profile.get_followers()]

# Retrieve the usernames of all followees
followees = [followee.username for followee in profile.get_followees()]
print(followers)Code language: Python (python)

Downloading Posts from Another Profile

Getting posts from any profile is easy in python. We just need to use get_posts(). I will this method on the profile of someone else. To download each post, we need to loop over the generator object using .download_post() method. Now let’s go through this:

# Load a new profile
profile = instaloader.Profile.from_username(bot.context, 'wwe')

# Get all posts in a generator object
posts = profile.get_posts()

# Iterate and download
for index, post in enumerate(posts, 1):
    bot.download_post(post, target=f"{profile.username}_{index}")Code language: Python (python)

It will save the post folder by creating its directory. In each folder, you will see the actual content of the posts of the profile like a video or images. I hope you liked this article on Instagram scraping with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to read more amazing articles.

Follow Us:

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498

Leave a Reply