登录到IMDb使用Selenium和Python。

huangapple go评论68阅读模式
英文:

Login into imdb with selenium and python

问题

我尝试使用Python Selenium登录"IMDB",但它无法正常进行,因为需要通过验证码并发送两步验证代码。如果您要使用自己的浏览器登录,就不需要填写验证码。

以下是您提供的代码:

def login(self, email, password='hi'):
        # sleep(30)
        self.get(SING_IN_URL)
        sleep(30)
        email_path = self.find_element(By.XPATH, EMAIL_BTN)
        email_path.click()
        email_path.send_keys(email)
        sleep(10)
        password_path = self.find_element(By.XPATH, PWD_BTN)
        password_path.click()
        password_path.send_keys(password)
        sleep(10)
        self.find_element(By.XPATH, '//*[@id="signInSubmit"]').click()
        sleep(10)

这是SING_IN_URL
https://www.imdb.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.imdb.com%2Fregistration%2Fap-signin-handler%2Fimdb_us&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=imdb_us&openid.mode=checkid_setup&siteState=eyJvcGVuaWQuYXNzb2NfaGFuZGxlIjoiaW1kYl91cyIsInJlZGlyZWN0VG8iOiJodHRwczovL3d3dy5pbWRi.lm....

我想知道为什么Selenium出现了问题,以及如何解决它?

英文:

I have tried logging into "IMDB" with python selenium but it doesn't happen correctly because it needs to pass the captcha and send a 2-step verification code. if you want to log in with your own browser it doesn't need to fill the captcha

screenshots: captcha,
2-step

and this is my code:

def login(self, email, password='hi'):
        # sleep(30)
        self.get(SING_IN_URL)
        sleep(30)
        email_path = self.find_element(By.XPATH, EMAIL_BTN)
        email_path.click()
        email_path.send_keys(email)
        sleep(10)
        password_path = self.find_element(By.XPATH, PWD_BTN)
        password_path.click()
        password_path.send_keys(password)
        sleep(10)
        self.find_element(By.XPATH, '//*[@id="signInSubmit"]').click()
        sleep(10)

and this is the SING_IN_URL :
https://www.imdb.com/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.imdb.com%2Fregistration%2Fap-signin-handler%2Fimdb_us&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=imdb_us&openid.mode=checkid_setup&siteState=eyJvcGVuaWQuYXNzb2NfaGFuZGxlIjoiaW1kYl91cyIsInJlZGlyZWN0VG8iOiJodHRwczovL3d3dy5pbWRiLmNvbS8_cmVmXz1sb2dpbiJ9&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&tag=imdbtag_reg-20

I want to know why it just happened for selenium. and How can I solve it?

答案1

得分: 1

一个简单的解决方法,可以避免登录表单和验证码,是加载一个已经登录的用户配置文件。然后,当你运行 driver.get(http://imdb.com/) 时,你已经登录,因为它使用用户配置文件的 cookie。在这里你可以找到关于如何在Chrome中创建用户配置文件的逐步教程。

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 2")

driver = webdriver.Chrome(..., options=options)
driver.get(http://imdb.com/)
# 现在你已经登录了

登录到IMDb使用Selenium和Python。

英文:

An easy workaround to avoid the login form and the captcha is to load a user profile in which you are already logged in. Then when you run driver.get(http://imdb.com/) you will be already logged in, because it uses the cookies of the user profile. Here you find a step by step tutorial on how to create a user profile in Chrome.

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 2")

driver = webdriver.Chrome(..., options=options)
driver.get(http://imdb.com/)
# now you are already logged in

登录到IMDb使用Selenium和Python。

huangapple
  • 本文由 发表于 2023年2月14日 05:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定