使用python和请求进行Instagram身份验证

编程入门 行业动态 更新时间:2024-10-25 04:15:15
本文介绍了使用python和请求进行Instagram身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要为我的项目创建instagram登录表单.我已经编写了此代码,但无法正常工作.请求后,我需要获取"sessionid" cookie

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 = 'www.instagram/' 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)

以下是响应标头:

`{'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': 'www.instagram/', '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'`}

以及req2.content中的零件代码:

<title>\n Page Not Found &bull; Instagram\n </title>

出什么问题了?预先谢谢你.

What is the problem? Thank you in advance.

推荐答案

如果使用 requests.Session() ,则不需要cookie标头.

对解决方案进行一些更改;您可以使用此:

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

import requests url = 'www.instagram/accounts/login/' url_main = url + 'ajax/' auth = {'username': 'login', 'password': 'pass'} headers = {'referer': "www.instagram/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('www.instagram/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'

更多推荐

使用python和请求进行Instagram身份验证

本文发布于:2023-10-10 01:28:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   python   Instagram

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!