I want to get sessionid from Instagram cookies using requests module.
I tried code which described in this question Instagram Authentification with python and requests but it doesn't work. is there any way how to get it? before or after login.
You can use this script to login to Instagram:
import re
import requests
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': '<USERNAME HERE>',
'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{time}:<PASSWORD HERE>',
'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)
print(r.url)
print(r.text)
print(s.cookies)
Prints (the sessionid is inside the cookies):
200
https://www.instagram.com/accounts/login/ajax/
{"user": true, "userId": "XXX", "authenticated": true, "oneTapPrompt": true, "reactivated": true, "status": "ok"}
<RequestsCookieJar[<Cookie csrftoken=XXX for .instagram.com/>, <Cookie ds_user_id=XXX for .instagram.com/>, <Cookie mid=XXX for .instagram.com/>, <Cookie rur=ATN for .instagram.com/>, <Cookie sessionid=<SESSION ID> for .instagram.com/>]>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With