Skip to content

Instantly share code, notes, and snippets.

@ycaty
Last active October 2, 2021 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ycaty/9bb38cb47f28a1599ba41c2caeffad93 to your computer and use it in GitHub Desktop.
Save ycaty/9bb38cb47f28a1599ba41c2caeffad93 to your computer and use it in GitHub Desktop.
Simple python 2.7 script for logging into instagram 2021
import requests
import time
import random
username = 'your_username_here'
password = 'your_password_here'
def default_headers():
return {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
'Accept-Language': 'en-US,en;q=0.8',
'Content-Length': '0',
'Host': 'www.instagram.com',
'Origin': 'https://www.instagram.com',
'Referer': 'https://www.instagram.com/',
'X-Instagram-AJAX': '1',
'X-Requested-With': 'XMLHttpRequest'}
def sleepy():
time.sleep(min(random.expovariate(0.6), 15.0))
#Set up our headers
headers = default_headers()
#Set up out payload ((#Insta requires enc_password to be posted like so))
enc_password = '#PWD_INSTAGRAM_BROWSER:0:{}:{}'.format(int(time.time()), password)
#Uncomment lines below to gain a lil insight ;^
#print (enc_password)
#print (headers)
session = requests.Session()
session.headers.update(headers)
session.cookies.update({'sessionid': '', 'mid': '', 'ig_pr': '1',
'ig_vw': '1920', 'ig_cb': '1', 'csrftoken': '',
's_network': '', 'ds_user_id': ''})
session.get('https://www.instagram.com/web/__mid/',timeout=17)
csrf_token = session.cookies.get_dict()['csrftoken']
session.headers.update({'X-CSRFToken': csrf_token})
#sleep for a random interval: Good to use this after every request to avoid bot detect
sleepy()
#Post our payload
login = session.post('https://www.instagram.com/accounts/login/ajax/', data={'enc_password': enc_password, 'username': username}, allow_redirects=True)
resp_json = login.json()
print (resp_json)
@ycaty
Copy link
Author

ycaty commented Oct 2, 2021

Once logged in u can save the cookies like so
cookies = session.cookies.get_dict()

and reload later

s = requests.Session()
s.cookies.update(cookies)
r = s.get("https://instagram.com")
print r.text #yay u should have username in here..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment