I need to create the instagram login form for my project. I've written this code but it doesnt work correctly. I need to get the 'sessionid' cookie after the request
def authorize_inst():
url = 'https://www.instagram.com/'
url_main = url + 'accounts/login/ajax/'
req1 = requests.get(url)
print(req1.headers)
print(req1.cookies['csrftoken'])
print('-----')
auth = {'username':'login','password':'pass'}
req2 = requests.post(url_main,cookies={'csrftoken':req1.cookies['csrftoken']},data=auth,allow_redirects=True)
print(req2.headers)
print(req2.cookies)
Here are the response headers:
`{'Content-Type': 'text/html', 'X-Frame-Options': 'SAMEORIGIN', 'Cache-Control': 'private, no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT', 'Vary': 'Cookie, Accept-Language', 'Content-Language': 'en', 'Access-Control-Allow-Origin': 'https://www.instagram.com/', 'Date': 'Sat, 17 Feb 2018 08:46:12 GMT', 'Strict-Transport-Security': 'max-age=86400', 'Set-Cookie': 'rur=FTW; Path=/, csrftoken=KSGEZBQrpQBQ8srEcK98teilzOsndDcF; expires=Sat, 16-Feb-2019 08:46:12 GMT; Max-Age=31449600; Path=/; Secure, mid=Wofr1AAEAAGPK-9pKoyWokm4jRo8; expires=Fri, 12-Feb-2038 08:46:12 GMT; Max-Age=630720000; Path=/', 'Connection': 'keep-alive', 'Content-Length': '21191'`}
And a part code from req2.content
:
<title>\n Page Not Found • Instagram\n </title>
What is the problem? Thank you in advance.
If you use
requests.Session()
, you won't need thecookie
header.Making a few changes to your solution; you can use this:
I've found the decision. Here is the code: