简体   繁体   English

使用 python-requests 登录 Instagram

[英]Logging in to instagram with python-requests

im trying to login to instagram using python-requests module and is not working for me, i've tried using someone else's function, tried changing headers, w/ & w/o session,我正在尝试使用 python-requests 模块登录 instagram,但对我不起作用,我尝试使用其他人的功能,尝试更改标题,w/&w/o 会话,

import requests

username = '*****'
password = '*****'

def login():
    s = requests.session()
    url = 'https://www.instagram.com/accounts/login/'
    headers1 = {
        'Host': 'graph.instagram.com',
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0',
        'Accept': '*/*',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate, br',
        'Referer': 'https://www.instagram.com/accounts/login/?source=auth_switcher',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': '7515',
        'Origin': 'https://www.instagram.com',
        'Connection': 'keep-alive',
        'Cookie': '****',
        'TE': 'Trailers'
    }
    data2 = {
        'username': username,
        'password': password
    }
    r2 = s.post(url, headers=headers1, data=data2)

    if username in r2.content or '***' in r2.content:
        print('Welcome, '+username+'')
    else:
        print('failed')
login()

need it to login

This will allow you to login with instagram:这将允许您使用 Instagram 登录:

    s = requests.Session()
    Grab = s.get("https://www.instagram.com/accounts/login/")
    Headd = {
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9',
        'content-type': 'application/x-www-form-urlencoded',
        'origin': 'https://www.instagram.com',
        'referer': 'https://www.instagram.com/accounts/login/',
        'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.2; zh) AppleWebKit/522.13.1 (KHTML, like Gecko) Version/3.0.2 Safari/522.13.1',
        'x-csrftoken': Grab.cookies.get_dict()['csrftoken'],
        'x-instagram-ajax': '1',
        'x-requested-with': 'XMLHttpRequest'
    }
    LoginData = {
        'username': user,
        'password': passw,
        'queryParams': '{}'
    }
    AccLogin = s.post('https://www.instagram.com/accounts/login/ajax/', headers=Headd, data=LoginData)
    # This is how you will check if you logged in
    if AccLogin.json()['authenticated']:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM