python爬蟲之利用selenium模塊自動(dòng)登錄CSDN
一、頁面分析
CSDN登錄頁面如下圖
二、引入selenium模塊及驅(qū)動(dòng)
2.1 并將安裝好的Chromedriver.exe引入到代碼中
# -*- coding:utf-8 -*- from selenium import webdriver import os 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)
2.2 瀏覽器驅(qū)動(dòng)引入
將驅(qū)動(dòng)下載后,復(fù)制chromedriver.exe 到谷歌瀏覽器的安裝路徑下,與Chrome.exe啟動(dòng)文件并列的目錄下:
三、爬蟲模擬登錄
3.1 設(shè)置網(wǎng)址鏈接
#設(shè)置瀏覽器需要打開的url url = "https://passport.csdn.net/login?code=public" browser.get(url)
3.2 切換到賬號(hào)密碼登錄
使用selenium模擬點(diǎn)擊 賬號(hào)密碼登錄的選項(xiàng)
#使用selenium選擇 賬號(hào)登錄按鈕 browser.find_element_by_link_text("賬號(hào)密碼登錄").click()
3.3 找到用戶名密碼的控件ID
3.4 注入用戶名和密碼
根據(jù)頁面代碼分析,獲得用戶名的id屬性為all,密碼的id屬性為password-number
使用python代碼,注入用戶名密碼
browser.find_element_by_id("all").clear() browser.find_element_by_id("all").send_keys("xxxx@gmail.com") time.sleep(2) browser.find_element_by_id("password-number").clear() browser.find_element_by_id("password-number").send_keys("1212121212")
3.5 模擬登錄點(diǎn)擊
分析頁面結(jié)構(gòu),模擬點(diǎn)擊登錄按鈕。
分析可獲得,登錄按鈕的class屬性為btn btn-primary,根據(jù)class來鎖定該按鈕
time.sleep(1) #增加一秒鐘的時(shí)間間隔 browser.find_element_by_css_selector("[class='btn btn-primary']").click()
四、成功登錄CSDN
五、完整代碼
# -*- coding:utf-8 -*- import os import time from selenium import webdriver # 從selenium導(dǎo)入webdriver from 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) #設(shè)置瀏覽器需要打開的url url = "https://passport.csdn.net/login?code=public" browser.get(url) browser.find_element_by_link_text("賬號(hào)密碼登錄").click() browser.find_element_by_id("all").clear() browser.find_element_by_id("all").send_keys("你的郵箱地址") time.sleep(1) browser.find_element_by_id("password-number").clear() browser.find_element_by_id("password-number").send_keys("你的登錄密碼") time.sleep(1) browser.find_element_by_css_selector("[class='btn btn-primary']").click()
到此這篇關(guān)于python爬蟲之利用selenium模塊自動(dòng)登錄CSDN的文章就介紹到這了,更多相關(guān)python自動(dòng)登錄CSDN內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python+opencv+selenium自動(dòng)化登錄郵箱并解決滑動(dòng)驗(yàn)證的問題
- Python如何基于selenium實(shí)現(xiàn)自動(dòng)登錄博客園
- Selenium+Python 自動(dòng)化操控登錄界面實(shí)例(有簡(jiǎn)單驗(yàn)證碼圖片校驗(yàn))
- selenium+python實(shí)現(xiàn)自動(dòng)化登錄的方法
- Python使用selenium實(shí)現(xiàn)網(wǎng)頁用戶名 密碼 驗(yàn)證碼自動(dòng)登錄功能
- python內(nèi)置函數(shù)frozenset()的使用小結(jié)
- selenium+python實(shí)現(xiàn)自動(dòng)登錄腳本
- python+selenium實(shí)現(xiàn)登錄賬戶后自動(dòng)點(diǎn)擊的示例
- Python用selenium實(shí)現(xiàn)自動(dòng)登錄和下單的項(xiàng)目實(shí)戰(zhàn)
相關(guān)文章
python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼
這篇文章主要介紹了python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼,簡(jiǎn)單介紹了實(shí)現(xiàn)步驟,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實(shí)例代碼
這篇文章主要介紹了python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實(shí)例代碼,需要的朋友可以參考下2018-05-05uwsgi+nginx部署Django項(xiàng)目操作示例
這篇文章主要介紹了uwsgi+nginx部署Django項(xiàng)目操作,結(jié)合實(shí)例形式簡(jiǎn)單介紹了uwsgi的概念、原理、安裝、項(xiàng)目創(chuàng)建、配置、調(diào)試運(yùn)行等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12Python通過TensorFLow進(jìn)行線性模型訓(xùn)練原理與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Python通過TensorFLow進(jìn)行線性模型訓(xùn)練原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python通過TensorFLow進(jìn)行線性模型訓(xùn)練相關(guān)概念、算法設(shè)計(jì)與訓(xùn)練操作技巧,需要的朋友可以參考下2020-01-01Python?matplotlib實(shí)戰(zhàn)之氣泡圖繪制
氣泡圖是一種多變量的統(tǒng)計(jì)圖表,可以看作是散點(diǎn)圖的變形,這篇文章主要為大家介紹了如何使用Matplotlib繪制氣泡圖,需要的小伙伴可以參考下2023-08-08基于Python實(shí)現(xiàn)wifi連接小程序
這篇文章主要為大家詳細(xì)介紹了如何使用Python編程語言編寫一個(gè)簡(jiǎn)單的連接Wi-Fi的程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01