Download Instagram profile pic using Python

Instagram is a photo and video-sharing social networking service owned by Facebook, Python provides powerful tools for web scraping of Instagram.

Modules required and Installation:

requests –



pip install requests

concept –
For a given user profile, open view-source and find “profile_pic_url_hd” . To find press ctrl+f and type “profile_pic_url_hd” the link with it is our data or profile pic.
link will look link :

https://scontent-bom1-1.cdninstagram.com/vp/d2df9b2d162969e87200984ee763cc27/5DC590F2/t51.2885-19/s320x320/61851740_845288152518430_7068999703693623296_n.jpg?_nc_ht=scontent-bom1-1.cdninstagram.com

Below is the stepwise implementation of the project –

Step 1: import all dependence

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Step 2: Ask for username and send a response to Instagram.

filter_none

edit
close

play_arrow

link
brightness_4
code

inta_username= input('enter username of instagram : ')
  
response = requests.get(f"{insta_url}/{inta_username}/")

chevron_right


Step 3: if the response is ok, find profile photo link

filter_none

edit
close

play_arrow

link
brightness_4
code

if response.ok:
    html=response.text
  
    bs_html=bs(html, features="lxml")
    bs_html=bs_html.text
    index=bs_html.find('profile_pic_url_hd')+21
  
    remaining_text=bs_html[index:]
    remaining_text_index=remaining_text.find('requested_by_viewer')-3
    string_url=remaining_text[:remaining_text_index]
  
    print(string_url, "\n \n downloading..........")

chevron_right


Step 4: Now, create a loop and download photo.

filter_none

edit
close

play_arrow

link
brightness_4
code

while True:
    filename='pic'+str(random.randint(1, 100000))+'.jpg'
    file_exists = os.path.isfile(filename)
  
    if not file_exists:
        with open(filename, 'wb+') as handle:
            response = requests.get(string_url, stream=True)
            if not response.ok:
                print(response)
            for block in response.iter_content(1024):
                if not block:
                    break
                handle.write(block)
    else:
        continue
    break
print("\n                downloading completed ..............")

chevron_right


Complete code :

filter_none

edit
close

play_arrow

link
brightness_4
code

import requests
from bs4 import BeautifulSoup as bs
import json
import random
import os.path
  
inta_username = input('enter username of instagram : ')
  
response = requests.get(f"{insta_url}/{inta_username}/")
  
if response.ok:
    html = response.text
    bs_html = bs(html, features ="lxml")
    bs_html = bs_html.text
    index = bs_html.find('profile_pic_url_hd')+21
    remaining_text = bs_html[index:]
    remaining_text_index = remaining_text.find('requested_by_viewer')-3
    string_url = remaining_text[:remaining_text_index]
  
    print(string_url, "\n \n downloading..........")
  
  
while True:
    filename ='pic'+str(random.randint(1, 100000))+'.jpg'
    file_exists = os.path.isfile(filename)
  
    if not file_exists:
        with open(filename, 'wb+') as handle:
            response = requests.get(string_url, stream = True)
            if not response.ok:
                print(response)
            for block in response.iter_content(1024):
                if not block:
                    break
                handle.write(block)
    else:
        continue
    break
print("\n                downloading completed ..............")

chevron_right


Output:



My Personal Notes arrow_drop_up

A 3rd-year Computer Science and Engineering undergraduate student at IERT, Allahabad with an interest in Programming, Data Science/AI and web development

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.