Skip to content Skip to sidebar Skip to footer

How Would I Log Into Instagram Using Beautifulsoup4 And Requests, And How Would I Determine It On My Own?

I've looked at these two posts on Stack Overflow so far: I can't login to Instagram with Requests and Instagram python requests log in without API. Both of the solutions don't work

Solution 1:

you can use this code to login to instagram

import re
import requests
from bs4 import BeautifulSoup

from datetime import datetime

link = 'https://www.instagram.com/accounts/login/'
login_url = 'https://www.instagram.com/accounts/login/ajax/'

time = int(datetime.now().timestamp())

payload = {
    'username': 'login',
    'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{time}:your_password',
    'queryParams': {},
    'optIntoOneTap': 'false'
}

with requests.Session() as s:
    r = s.get(link)
    csrf = re.findall(r"csrf_token\":\"(.*?)\"", r.text)[0]
    r = s.post(login_url, data=payload, headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
        "Referer": "https://www.instagram.com/accounts/login/",
        "x-csrftoken": csrf
    })
    print(r.status_code)

Solution 2:

Hint: I needed to modify the line

r = s.get(link) 

into

r = s.get(link,headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})

to get a proper reply. Without it, I got "page not found" using JupyterNotebook.

Post a Comment for "How Would I Log Into Instagram Using Beautifulsoup4 And Requests, And How Would I Determine It On My Own?"