directory
Page analysis
Introduce Selenium modules and drivers
1, and the installed Chromedriver.exe into the code
2. Browser driver introduction
Crawler simulation login
1, set up the url link
2. Switch to the account and password
3. Find the control ID for the username and password
4. Inject the user name and password
5. Simulate login click
You have logged in to the CSDN successfully
The first effect
Page analysis
The following figure shows the CSDN login page
Introduce Selenium modules and drivers
1, and the installed Chromedriver.exe into the code
# -*- coding: UTF-8 -*- from Selenium import webdriver import OS import time # chromedriver="C:/Users/lex/AppData/Local/Google/Chrome/Application/chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver browser = webdriver.Chrome(chromedriver)Copy the code
2. Browser driver introduction
Driver download address:
Download.csdn.net/download/we…
After downloading the driver, copy chromedriver.exe to the same directory as the Chrome.exe startup file in the Google Chrome installation directory:
Crawler simulation login
1, set up the url link
# set the browser need to open the url url = "https://passport.csdn.net/login?code=public" the get (url)Copy the code
2. Switch to the account and password
Use selenium to simulate the option of clicking on an account password to log in
# Use selenium select account login button browser.find_element_by_link_text(" account password login ").click()Copy the code
3. Find the control ID for the username and password
4. Inject the user name and password
According to the page code analysis, the id attribute of the user name is all, and the ID attribute of the password is password-number
Using Python code, inject the username and password
browser.find_element_by_id("all").clear()
browser.find_element_by_id("all").send_keys("[email protected]")
time.sleep(2)
browser.find_element_by_id("password-number").clear()
browser.find_element_by_id("password-number").send_keys("1212121212")
Copy the code
5. Simulate login click
Analyze the page structure and simulate clicking the login button.
The class attribute of the login button is BTN bTN-primary, and the button is locked according to the class
Browser.find_element_by_css_selector ("[class=' BTN btn-primary']").click()Copy the code
You have logged in to the CSDN successfully
The complete code
# -* -coding: UTF-8 -* -import OS import time from Selenium import webDriver selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options import Json import time # chromedriver.exe chromedriver="C:/Users/lex/AppData/Local/Google/Chrome/Application/chromedriver.exe" Os. environ["webdriver.chrome.driver"] = ChromeDriver browser = webDriver.Chrome(ChromeDriver) # set the url that the browser needs to open "Https://passport.csdn.net/login?code=public" the get (url) the find_element_by_link_text (" account password "). Click () Browser.find_element_by_id ("all").clear() browser.find_element_by_id("all").send_keys(" Your email address ") time.sleep(1) Browser.find_element_by_id ("password-number").clear() browser.find_element_by_id("password-number").send_keys(" Your login password ") time.sleep(1) browser.find_element_by_css_selector("[class='btn btn-primary']").click()Copy the code