Upload Instagram profile photo using Python

Required modules and installation:

Requests —

 pip install requests 

concept —
For this user profile, open view-source and find “profile_pic_url_hd” . To find press Ctrl + F and type “profile_pic_url_hd” link with it — our data or profile picture. 
link will look at link:

 https://scontent-bom1-1.cdninstagram.com/vp/d2df9b2d162969e87200984ee763cc27/5DC590F2/t51.2885-19/s320x320/61851740_8452881599327036_7068=jht scontent-bom1-1.cdninstagram.com 

Below is a step-by-step implementation of the project —

Step 1: import the entire dependency

import requests

from bs4 import BeautifulSoup as bs

import json

import random

import os.path < / code>

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

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 answer is ok, find the link to the profile photo

if response.ok:

html = response.text

  

  bs_html = bs (html, features = "lxml" )

bs_html = bs_html.text

index = bs_html. find ( `pro file_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, "download ing .......... " )

Step 4: Now create a loop and upload a photo.

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 ( " downloading completed .............." )

Full code:

import requests

from bs4 import BeautifulSoup as bs

import json

import random

import os.path

 

insta_url = ` https://www.instagram .com `

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, " 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)

f or block in response.iter_content ( 1024 ):

if not block:

  break

handle.write (block )

else :

continue

break

print ( " downloading completed .............." )

Exit :