首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用机械化python登录instagram

使用机械化python登录instagram
EN

Stack Overflow用户
提问于 2017-04-19 07:08:44
回答 1查看 895关注 0票数 1

在过去的一段时间里,我一直在尝试使用机械化python登录我的instagram账户,但由于某种原因,它并不起作用。

为了检查我是否正确登录,我决定使用"br.geturl()“检查url,一旦登录成功,它应该显示为"https://www.instagram.com/”,但在我运行程序后,它只显示为:"https://www.instagram.com/accounts/login/username=username_here&password=password_here“。

有人知道怎么解决这个问题吗?

注意:我知道我的登录信息是正确的。

下面是我的代码:

代码语言:javascript
复制
import mechanize

br = mechanize.Browser()
url = "https://www.instagram.com/accounts/login/"
br.set_handle_robots(False)
response = br.open(url)

f = list(br.forms())

br.form = f[0]
print br.form


br.form["username"] = 'username_goes_here'
br.form["password"] = 'password_goes_here'

br.submit()

print br.geturl()
EN

回答 1

Stack Overflow用户

发布于 2018-10-22 00:00:04

因为表单是通过JavaScript生成的。所以在html上找不到它。其中一种方法是使用Selenium Webdriver。

代码语言:javascript
复制
from selenium import webdriver

driver = webdriver.Chrome('/Usr/chromedriver')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
username = driver.find_element_by_xpath('//*[@name="username"]')
password = driver.find_element_by_xpath('//*[@name="password"]')
login_btn = driver.find_element_by_xpath('//*[@class="oF4XW sqdOP  L3NKy      "]')

username.send_keys("username")
password.send_keys("password")

#test to see if input values are reflecting
print(username.get_attribute('value'))
print(password.get_attribute('value'))

#login
login_btn.click()
logged_in_class = driver.find_elements_by_class_name("logged-in")
not_logged_in_class = driver.find_elements_by_class_name("not-logged-in")

#to check if logged-in or not-logged-in
print(len(logged_in_class))
print(len(not_logged_in_class))


driver.quit()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43483662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档