简体   繁体   English

我如何使用 python 请求登录 instagram

[英]How can i login in instagram with python requests

Nowdays i am trying to make a simple python program that logs you into your instagram with requests library but it always prints wrong even when the pass is right thats the code.现在,我正在尝试制作一个简单的 Python 程序,该程序将您通过 requests 库登录到您的 Instagram 中,但即使通过正确的代码,它也总是打印错误。

import requests
url = 'www.instagram.com/accounts/login/ajax'
values = {'username' : 'USERNAME',
          'password' : 'thepassword'
try:
    session = requests.session()
    session.post(url, data=values)
except:
  print("wrong")

Simply making a POST request with username and password won't log you into Instagram.仅使用用户名和密码发出POST请求不会让您登录 Instagram。 There are many other parameters you would need to post along with it.还有许多其他参数需要与它一起发布。 Though it is possible to do this with requests (by tracking network requests and then making POST requests according to it), there are far better options like using the selenium library.虽然可以通过requests来做到这一点(通过跟踪网络请求,然后根据它发出POST请求),但还有更好的选择,比如使用selenium库。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("path/to/chromedriver")

try:
    driver.get("https://www.instagram.com/accounts/login")
    login_credentials = driver.find_elements_by_css_selector(
    "._2hvTZ.pexuQ.zyHYP")
    login_credentials[0].send_keys("ENTER YOUR USERNAME")
    login_credentials[1].send_keys("ENTER YOUR PASSWORD")
    click_login = driver.find_element_by_css_selector(
    "._0mzm-.sqdOP.L3NKy").click()
    print "LOG IN SUCCESS"
except:
    print "SOMETHING WENT WRONG"

You can make the browser headless if you don't want a browser to open.如果您不想打开浏览器,您可以使浏览器无头。 Instagram is decrypting its API. Instagram 正在解密其 API。 However, there are many private Instagram APIs available on Github.但是,Github有许多私有的Instagram API

You have to register your app and then use the token you get as a token in every request .您必须注册您的应用程序,然后在每个请求中使用您获得的令牌作为令牌。

I WOULD NOT SUGGEST DOING THAT , as Instagram is changing its API to the Instagram Web API .我不建议这样做,因为Instagram 正在将其 API更改为Instagram Web API

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

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