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