在本文中,我们将看到如何使用 python 构建一个简单的自动登录机器人。
在这种情况下,每个网站都使用身份验证,我们必须通过输入正确的凭据进行登录。但有时一次又一次地登录特定网站会变得非常忙碌。因此,为了解决这个问题,让我们使用 python 构建我们自己的自动登录机器人。
我们将使用Selenium(python 库)来制作自动登录机器人。Python Selenium 库帮助我们访问 Selenium WebDriver 的所有功能,如 Firefox、Chrome、Remote 等。
安装
首先,我们必须使用以下命令安装 selenium:
pip install selenium
selenium 安装成功后,我们还需要安装 chromedriver 来访问 selenium 的 chrome webdriver。您可以从这里下载相同的版本(根据您的系统 chrome 版本和您的操作系统下载版本)。
确保您已记下 chromedriver 的下载位置(因为它在我们的 python 脚本中使用)。现在下载后解压缩 zip 文件,请注意解压缩文件的文件位置,因为我们稍后在 python 代码中需要它。(您可以通过单击属性然后单击详细信息来找到该位置)。
分步实施:
首先从 selenium 库导入 webdriver。
找到您要登录的登录页面的 URL。
向 selenium webdriver 提供位置可执行的 chrome 驱动程序以访问 chrome 浏览器。
最后,通过右键单击用户名和密码的检查,找到用户名和密码的名称或 ID 或类或 CSS 选择器。

# Used to import the webdriver from selenium
from selenium import webdriver
import os
# Get the path of chromedriver which you have install
def startBot(username, password, url):
path = "C:\\Users\\hp\\Downloads\\chromedriver"
# giving the path of chromedriver to selenium websriver
driver = webdriver.Chrome(path)
# opening the website in chrome.
driver.get(url)
# find the id or name or class of
# username by inspecting on username input
driver.find_element_by_name(
"id/class/name of username").send_keys(username)
# find the password by inspecting on password input
driver.find_element_by_name(
"id/class/name of password").send_keys(password)
# click on submit
driver.find_element_by_css_selector(
"id/class/name/css selector of login button").click()
# Driver Code
# Enter below your login credentials
username = "Enter your username"
password = "Enter your password"
# URL of the login page of site
# whcih you want to automate login.
url = "Enter the URL of login page of website"
# Call the function
startBot(username, password, url)
效果图

本文链接:https://it72.com/12696.htm