P
P
Ponteley2020-07-13 14:45:31
Python
Ponteley, 2020-07-13 14:45:31

How to login to instagram with python requests?

Hi, I'm trying to login to Instagram using python requests.

from bs4 import BeautifulSoup as bs
import requests


BASE_URL = 'https://www.instagram.com/'
LOGIN_URL = BASE_URL + 'accounts/login/ajax/'

headers = {
    'Host': 'www.instagram.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
    'Accept': '*/*',
    'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding': 'gzip, deflate, br',
    'Referer': 'https://www.instagram.com/',
    'Cookie': '',
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'TE': 'Trailers'
}



session = requests.Session()
session.headers.update(headers)
responce = session.get(BASE_URL)
soup = bs(responce.content, "html.parser")
csrf = responce.cookies['csrftoken']
session.headers.update({'X-CSRFToken': csrf})

data = {'username': "login", 'password': "pass"}

login = session.post(LOGIN_URL, data=data, allow_redirects=True)
print(login)


But it gives an error 400. How to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-07-13
@SoreMix

You are passing the wrong parameters, look in the debugger for what you need

F
Finnick is healthy, 2020-07-14
@phinnik

Your cookies are an empty string, of course you will not enter anywhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question