Download Instagram profile pic using Python

16 Jun, 2022
Instagram is a photo and video-sharing social networking service owned by Facebook, Python provides powerful tools for web scraping of Instagram. 
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. The link will look like :  
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  
Python3
import requestsfrom bs4 import BeautifulSoup as bsimport jsonimport randomimport os.path
Step 2: Ask for username and send a response to Instagram.  
Python3
insta_url='https://www.instagram.com'inta_username= input('enter username of instagram : ') response = requests.get(f"{insta_url}/{inta_username}/")
Step 3: if the response is ok, find profile photo link  
(Note: replace ‘\\u0026’ with ‘&’ in the string_url to remove bad URL timestamp or bad URL hash error) 
Python3
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].replace("\\u0026","&")     print(string_url, "\n \n downloading..........")
Step 4: Now, create a loop and download photo.  
Python3
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    breakprint("\n                downloading completed ..............")
Output:  
Other Method : We can download profile pic of any Instagram account using instaloader module by just giving Instagram handle of the user.
First we need to install instaloader module :
pip install instaloader 
Example:
Python3
import instaloader ig = instaloader.Instaloader()dp = input("Enter Insta username : ") ig.download_profile(dp , profile_pic_only=True)
Output :  Profile pic will be downloaded in  the same directory when we enter the input user id.
hammadbasithb
imsushant12
surinderdawra388
Project
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Implementing Web Scraping in Python with BeautifulSoup
10 Best Web Development Projects For Your Resume
OpenCV C++ Program for Face Detection
Simple Chat Room using Python
Twitter Sentiment Analysis using Python
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
Python Dictionary
How to get column names in Pandas dataframe