Tutorials  Articles  Notifications  Login  Signup


RK

Rajan Kumar

Founder at HackersFriend Updated May 16, 2020, 10:40 p.m. ⋅ 5011 views

Building Instagram Profile pic downloader in python


We can use python to download profile pic of any instagram user by using web scraping. In this article I'll build an simple Instagram profile pic downloader.

We'll use following python packages to do our work.

  • requests
  • bs4
  • os

This is how our script is gonna look like.

Package os comes in-built with python. It will be used to access path where we'll store our downloaded profile pic. We'll have to download requests and bs4 though.

We can do it using pip.

pip install requests, bs4

Once we are done with installation of required packages, we can proceed with our script.

Understand Instagram's web page contents

You can open HTML source code of any users instagram profie page and look for "profile_pic_url_hd". You can do so by pressing ctl + f and searching in source code. Values of this json attribute is contains the url of user's profile pic.

We'll parse out the value then call this url to get the image.

import os.path
import requests 
from bs4 import BeautifulSoup as bs


home ='https://www.instagram.com/'
username = input('Username whose DP you want to downlaod: ')
finalUrl = home + username + "/"

response = requests.get(finalUrl)  # Fetch the user's profile page

if response.ok:
    try:
        html = response.text
        soup = bs(html, features ="lxml") 
        soup_text = soup.text # Convert the entire page data in text string
        start_index = soup_text.find('profile_pic_url_hd')+21
        remaining_text = soup_text[start_index:] # rest of the data after 'profile_pic_url_hd":"'
        last_index = remaining_text.find('requested_by_viewer')-3
        image_url = remaining_text[:last_index]

        print("The image url is ", image_url)

Downlaoding the profile pic

Once, we get out the value of profile pic from page we can call this url to download image.

import os.path
import requests 
from bs4 import BeautifulSoup as bs


home ='https://www.instagram.com/'
username = input('Username whose DP you want to downlaod: ')
finalUrl = home + username + "/"

response = requests.get(finalUrl)  # Fetch the user's profile page

if response.ok:
    try:
        html = response.text
        soup = bs(html, features ="lxml") 
        soup_text = soup.text # Convert the entire page data in text string
        start_index = soup_text.find('profile_pic_url_hd')+21
        remaining_text = soup_text[start_index:] # rest of the data after 'profile_pic_url_hd":"'
        last_index = remaining_text.find('requested_by_viewer')-3
        image_url = remaining_text[:last_index]

       # print("The image url is ", image_url)
        print("Iniating Download.....") 

        filename=username+'_insta_dp_'+'.jpg'
        with open(filename, 'wb+') as handle:
            response = requests.get(image_url.replace("\\u0026","&"))
            if not response.ok:
                print("Cannot download something went wrong.")
            else:
                handle.write(response.content)

        print("Download complete")
    except:

 

Once you are ready with the script you can give any instagram username to the script and it will download that user's profile pic.

enter username of instagram : google
String url:  https://instagram.fpat1-1.fna.fbcdn.net/v/t51.2885-19/s320x320/95014083_714049196089682_7323048346396917760_n.jpg?_nc_ht=instagram.fpat1-1.fna.fbcdn.net\u0026_nc_ohc=QtPHnO7rKDwAX-T6O6u\u0026oh=dc8e14af7e7262ec98cb2c81347a7c35\u0026oe=5EE34D8B


 downloading..........



 downloading..........

 Download complete..........

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account