问题
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