Error logging into instagram with python

孤人 提交于 2020-01-04 05:10:46

问题


I am trying to log into my instagram via a python script using argparse. It seems to connect but it prints out "This page could not be loaded. If you have cookies disabled in your browser, oryou are browsing in Private Mode, please try enabling cookies or turning off Private Mode, and then retrying your action." Here's my code:

import argparse
import mechanicalsoup
from bs4 import BeautifulSoup

parser = argparse.ArgumentParser(description='Login to Instagram.')
parser.add_argument("username")
parser.add_argument("password")
args = parser.parse_args()

browser = mechanicalsoup.Browser()

login_page = browser.get("https://instagram.com/accounts/login/?force_classic_login=&next=/oauth/authorize/?client_id=9d836570317f4c18bca0db6d2ac38e29%26redirect_uri=http://websta.me/%26response_type=code%26scope=comments%2Brelationships%2Blikes")

# we grab the login form
login_form = login_page.soup.select(".dialog-main")[0].select("#login-form")[0]

# specify username and password
login_form.select("#id_username")[0]['value'] = args.username
login_form.select("#id_password")[0]['value'] = args.password

#submit
page2 = browser.submit(login_form, login_page.url)
#verify login
p = page2.soup.p.string
print(p)
print(args.password)

回答1:


There's a library to access instagram from python. To login, you need the following code:

from instagram.client import InstagramAPI

access_token = "YOUR_ACCESS_TOKEN" # get this from instagram
client_secret = "YOUR_CLIENT_SECRET" # this, too, from instagram
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
for media in recent_media:
    print media.caption.text

In other words, don't reinvent the wheel.




回答2:


See this answer you need to also include the client secret now to login to the API using that library.

Debugging Python Instagram API client - 'NoneType' has no len()



来源:https://stackoverflow.com/questions/30389316/error-logging-into-instagram-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!