Python Script for Just logging in to Instagram

1.3k views Asked by At

Here I have written an Python script to login in to Instagram...but problem is that i can not get cookies and post them again so it is giving me error to enable cookies before login so please someone help me at this script

import requests
import re
import cookielib

emsg='you are browsing in Private Mode'
user_agent = {'User-agent': 'Mozilla/5.0'}

url = 'https://instagram.com/'
url2 = 'https://instagram.com/accounts/login/'
values = {'username': 'username',
              'password': 'password'}

session = requests.Session()

jar1=requests.post(url2,headers=user_agent)
print jar1.cookies
r = requests.post(url2,cookies=jar1.cookies,headers = user_agent, data=values)

print r.request.headers['Cookie']
target = open('filename.html', 'w')
target.write(r.content)
target.close ()


if re.findall(emsg,r.content):
    print "\n\n\n\n Enable cookies please\n\n\n\n"
else:
    print"\n\n\n\nSomething is changed\n\n\n\n"
1

There are 1 answers

5
Vikas Ojha On

Requests session object automatically takes care of cookies -

import requests
import re

emsg='you are browsing in Private Mode'
user_agent = {'User-agent': 'Mozilla/5.0'}

url = 'https://instagram.com/'
url2 = 'https://instagram.com/accounts/login/'
values = {'username': 'username',
              'password': 'password'}

session = requests.Session()
session.headers.update(user_agent)
session.get(url2)

r = session.post(url2, data=values)

print r.request.headers['Cookie']
target = open('filename.html', 'w')
target.write(r.content)
target.close()

if re.findall(emsg,r.content):
    print "\n\n\n\n Enable cookies please\n\n\n\n"
else:
    print"\n\n\n\nSomething is changed\n\n\n\n"