在过去的一段时间里,我一直在尝试使用机械化python登录我的instagram账户,但由于某种原因,它并不起作用。
为了检查我是否正确登录,我决定使用"br.geturl()“检查url,一旦登录成功,它应该显示为"https://www.instagram.com/”,但在我运行程序后,它只显示为:"https://www.instagram.com/accounts/login/username=username_here&password=password_here“。
有人知道怎么解决这个问题吗?
注意:我知道我的登录信息是正确的。
下面是我的代码:
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()
发布于 2018-10-22 00:00:04
因为表单是通过JavaScript生成的。所以在html上找不到它。其中一种方法是使用Selenium Webdriver。
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()
https://stackoverflow.com/questions/43483662
复制相似问题