Python實現(xiàn)獲取網頁內容及自動填表單與登錄功能
食用前準備
python 3.10.10 #二維碼的庫ddddocr 需要
庫
import time import ddddocr
源碼
# import threading # 導入threading模塊 # from Feishu_SendMsg import * # Identification verification code import time import ddddocr interval = 100 * 60 # def delayCall(): # 定義方法 # SendMsg("選題 快快快!!!") # timer=threading.Timer(interval,delayCall) # 每秒運行 # timer.start() # 執(zhí)行方法 # if __name__ == '__main__': # # t1=threading.Timer(interval,function=delayCall) # 創(chuàng)建定時器 # t1.start() # 開始執(zhí)行線程 from selenium import 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.common.keys import Keys # SendMsg("自動填表單") options = webdriver.ChromeOptions() options.add_argument('--enable-automation') options.add_argument('--no-sandbox') options.add_argument('--disable-extensions') options.add_argument('--start-maximized') options.add_argument('--disable-infobars') prefs = {"profile.default_content_setting_values.autocomplete_enabled": 2} options.add_experimental_option("prefs", prefs) # SendMsg("創(chuàng)建 Chrome 瀏覽器實例") # 創(chuàng)建 Chrome 瀏覽器實例 browser = webdriver.Chrome(options=options) # SendMsg("打開網頁") browser.get('www.tttttttt.com') # SendMsg("找到賬號和密碼框元素并輸入指定字符串") username = browser.find_element("name","username") password = browser.find_element("name","userpass") usercode = browser.find_element("name","usercode") img_verifycode = browser.find_element("id","img_verifycode") # SendMsg("自動填充賬號密碼") username.send_keys("11111") password.send_keys("11111") verifycodeBase64 = img_verifycode.screenshot_as_base64 ocr = ddddocr.DdddOcr() res = ocr.classification(verifycodeBase64) usercode.send_keys(res) # SendMsg(f"識別并填寫驗證碼: {res}") # SendMsg("提交表單") password.send_keys(Keys.RETURN) # SendMsg("登陸: 提交表單")
知識點補充
下面為大家介紹一下文中用到的ddddocr庫的相關使用吧
識別驗證碼的python 庫有很多,用起來也并不簡單,ddddocr (帶帶弟弟ocr)庫是一個簡單實用的識別驗證碼的庫,推薦給大家
ddddocr具體使用方法
import os import ddddocr from time import sleep from PIL import Image from selenium import webdriver from selenium.webdriver.common.by import By class GetVerificationCode: def __init__(self): self.res = None url = '要登錄的地址' self.driver = webdriver.Chrome() self.driver.maximize_window() # 將瀏覽器最大化 self.driver.get(url) # 獲取驗證碼信息 def getVerification(self): # 獲取當前文件的位置、并獲取保存截屏的位置 current_location = os.path.dirname(__file__) screenshot_path = os.path.join(current_location, "..", "VerificationCode") # 截取當前網頁并放到自定義目錄下,并命名為printscreen,該截圖中有我們需要的驗證碼 sleep(1) self.driver.save_screenshot(screenshot_path + '//' + 'printscreen.png') sleep(1) # 定位驗證碼 imgelement = self.driver.find_element(By.XPATH, '驗證碼圖片的Xpath定位') # 獲取驗證碼x,y軸坐標 location = imgelement.location # 獲取驗證碼的長寬 size = imgelement.size # 寫成我們需要截取的位置坐標 rangle = (int(location['x'] + 430), int(location['y'] + 200), int(location['x'] + size['width'] + 530), int(location['y'] + size['height'] + 250)) # 打開截圖 i = Image.open(screenshot_path + '//' + 'printscreen.png') # 使用Image的crop函數(shù),從截圖中再次截取我們需要的區(qū)域 fimg = i.crop(rangle) fimg = fimg.convert('RGB') # 保存我們截下來的驗證碼圖片,并讀取驗證碼內容 fimg.save(screenshot_path + '//' + 'code.png') ocr = ddddocr.DdddOcr() with open(screenshot_path + '//' + 'code.png', 'rb') as f: img_bytes = f.read() self.res = ocr.classification(img_bytes) print('識別出的驗證碼為:' + self.res) # 判斷驗證碼錯誤時的提示信息是否存在 def isElementPresent(self, by, value): try: element = self.driver.find_element(by=by, value=value) except NoSuchElementException: pass # 發(fā)生了NoSuchElementException異常,說明頁面中未找到該元素,返回False return False else: # 沒有發(fā)生異常,表示在頁面中找到了該元素,返回True return True # 登錄 def login(self): self.getVerification() self.driver.find_element(By.XPATH, '用戶名輸入框Xpath定位').send_keys('用戶名') self.driver.find_element(By.XPATH, '密碼輸入框Xpath定位').send_keys('密碼') self.driver.find_element(By.XPATH, '驗證碼輸入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click() sleep(2) isFlag = True while isFlag: try: isPresent = self.isElementPresent(By.XPATH, '驗證碼錯誤時的提示信息Xpath定位') if isPresent is True: codeText = self.driver.find_element(By.XPATH, '驗證碼錯誤時的提示信息Xpath定位').text if codeText == "驗證碼不正確": self.getVerification() sleep(2) self.driver.find_element(By.XPATH, '驗證碼輸入框Xpath定位').clear() sleep(1) self.driver.find_element(By.XPATH, '驗證碼輸入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click() sleep(2) tips = self.driver.find_element(By.XPATH, '未輸入驗證碼時的提示信息Xpath定位').text if tips == "請輸入驗證碼": self.getVerification() sleep(2) self.driver.find_element(By.XPATH, '驗證碼輸入框Xpath定位').click() sleep(1) self.driver.find_element(By.XPATH, '驗證碼輸入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登錄按鈕Xpath定位').click() sleep(2) continue else: print("驗證碼正確,登錄成功!") except NoSuchElementException: pass else: isFlag = False sleep(5) self.driver.quit() if __name__ == '__main__': GetVerificationCode().login()
識別結果
到此這篇關于Python實現(xiàn)獲取網頁內容及自動填表單與登錄功能的文章就介紹到這了,更多相關Python獲取網頁內容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
利用OpenCV+Tensorflow實現(xiàn)的手勢識別
這幾天沒事,想著再學點一些視覺識別方向的東西,因為之前做了驗證碼識別,有了機器學習的信心,因此這次打算做個手勢識別,下面這篇文章主要給大家介紹了關于利用OpenCV+Tensorflow實現(xiàn)的手勢識別的相關資料,需要的朋友可以參考下2022-11-11python實現(xiàn)合并多個list及合并多個django QuerySet的方法示例
這篇文章主要介紹了python實現(xiàn)合并多個list及合并多個django QuerySet的方法,結合實例形式分析了Python使用chain合并多個list以及合并Django中多個QuerySet的相關操作技巧,需要的朋友可以參考下2019-06-06Django中QuerySet查詢優(yōu)化之prefetch_related詳解
prefetch_related()和select_related()的設計目的很相似,都是為了減少SQL查詢的數(shù)量,但是實現(xiàn)的方式不一樣,下面這篇文章主要給大家介紹了關于Django中QuerySet查詢優(yōu)化之prefetch_related的相關資料,需要的朋友可以參考下2022-11-11