Change language

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 :

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method