Instagram Authentification with python and request

2019-06-06 10:46发布

问题:

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 &bull; Instagram\n                </title>

What is the problem? Thank you in advance.

回答1:

If you use requests.Session(), you won't need the cookie header.

Making a few changes to your solution; you can use this:

import requests

url = 'https://www.instagram.com/accounts/login/'
url_main = url + 'ajax/'
auth = {'username': 'login', 'password': 'pass'}
headers = {'referer': "https://www.instagram.com/accounts/login/"}

with requests.Session() as s:
    req = s.get(url)
    headers['x-csrftoken'] = req.cookies['csrftoken']
    s.post(url_main, data=auth, headers=headers)
    # Now, you can use 's' object to 'get' response from any instagram url
    # as a logged in user.
    r = s.get('https://www.instagram.com/accounts/edit/')
    # If you want to check whether you're getting the correct response,
    # you can use this:
    print(auth['username'] in r.text)  # which returns 'True'


回答2:

I've found the decision. Here is the code:

import requests

url = 'https://www.instagram.com/accounts/login/'
url_main = url + 'ajax/'
req1 = requests.get(url)
auth = {'username': 'login', 'password': 'pass'}
headers = {
    'origin': "https://www.instagram.com",
    'x-instagram-ajax': "1",
    'content-type': "application/x-www-form-urlencoded",
    'accept': "*/*",
    'x-requested-with': "XMLHttpRequest",
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36",
    'x-csrftoken': req1.cookies['csrftoken'],
    'referer': "https://www.instagram.com/accounts/login/",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
    'cookie': "fbm_124024574287414=base_domain=.instagram.com; ig_or=landscape-primary; ig_pr=1; ig_vw=1920; mid=Wcp3vAALAAGjSka8GEnPwijnia6-; rur=FTW; ig_vh=949; csrftoken="+req1.cookies['csrftoken']+"; fbsr_124024574287414=jSVsTpoGuOgZQB0vEP_X70hrO2-LlfD9EnUz9nwGTXo.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiJBUUMyM1FOT2ZwQU1oRVVudldzeGp1dHpwckEyODBLbUZseVo4VjVMMVVRVkJYbUVadHFyd25nekdtdzg2ejFTajRIYzVSWVRISHlvTjZXU29ScEdDZXB5RnRTMDloRXlLT3dXbU5uTTloQV9PTFE2VUI2ZFhPWW5Qa3pBNlNSZkFpSWZiU2N2anEyRFZna2FMdkdDWkRBQklCbElhYVAya2JNZzJBQW9fU0lzS3Z5NDhHRXB2RjFwQmdKOHNrdjltZWtYbFF1Z1dib040UXlzM2lwUTVfRUsxTjJUaHBpb3g1QUF2SDNpSVE2Qm1fdTFSeTZTVHFZMWR1M2NCSU5FRHpiZXRaRjFvSXY1MGJzLWFWQk4tcmFsVHY1dGE2VS13ajZCUXE0UlFEQjVHZEdqeDZpZkdlc0JsYnZvQUNlVFFJQ3pVSl9id1F1eGpyc0UxbEFzalRWZCIsImlzc3VlZF9hdCI6MTUxODg4NDA1MCwidXNlcl9pZCI6IjEwMDAyMzcyMDI5NTQyNyJ9",
    'x-compress': "null",
    'cache-control': "no-cache"
}
req2 = requests.post(url_main,data=auth,headers=headers)
sessionid = req2.cookies['sessionid']

print(req2.content)
print(req2.cookies['ds_user_id'])
print(req2.cookies['sessionid'])