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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically