python中selenium(模拟登陆)+pytesseract(自动识别验证码)应用例子之查询住房公积金
生活随笔
收集整理的這篇文章主要介紹了
python中selenium(模拟登陆)+pytesseract(自动识别验证码)应用例子之查询住房公积金
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里以查詢深圳住房公積金為例(http://gjj.sz.gov.cn/fzgn/zfcq/)
from selenium import webdriver # 模擬瀏覽器操作 from PIL import Image # 圖像處理 import time import pytesseract # 驗證碼識別 # 加載等待時用到 from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC# 獲取公積金余額信息,輸入:電腦號,身份證號 def getInfo(computerID,IDnumber):'''1.頁面請求'''# http://gjj.sz.gov.cn/fzgn/zfcq/ 經查看左邊原網址iframe標簽發現輸入框嵌入了下面的地址url = "http://app.szzfgjj.com:7001/pages/sj_yecx.jsp" browser = webdriver.Firefox()browser.get(url)browser.maximize_window() # 最大化#print(browser.page_source)# 等待加載完成#time.sleep(1)# 顯示等待5s,等待元素加載(等某個條件發生后再繼續執行后續代碼)# 顯示等待和隱式等待區別:https://blog.csdn.net/cyjs1988/article/details/76033180try:locator = (By.ID,"rad_1")WebDriverWait(browser,5).until(EC.presence_of_element_located(locator))except: # 超時則提示加載失敗,瀏覽器退出并返回print("load fail") browser.quit()return'''2.輸入信息'''# 選中個人社保電腦號按鈕 browser.find_element_by_id("rad_1").click()# 輸入社保電腦號ComputerNumber_input = browser.find_element_by_id("accnum1")ComputerNumber_input.send_keys(computerID)# 獲取身份證輸入框IDNumber_input = browser.find_element_by_id("certinum1")# 輸入身份證號碼IDNumber_input.send_keys(IDnumber)'''3.自動識別驗證碼處理'''# 找到驗證碼位置VerifyCode = browser.find_element_by_id("imgyzm")location = VerifyCode.location# 獲取驗證碼的sizesize = VerifyCode.size#寫成我們需要截取的位置坐標 ,這里獲得(x,y)為矩形左下角的坐標,(x+width,y+height)為對應右上角的坐標rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #print(rangle)# 先保存整個網頁截圖name = "fullpage.png"browser.save_screenshot(name)# 通過PIL中的crop剪切圖片image = Image.open(name)codeArea = image.crop(rangle) # 使用Image的crop函數(對圖片進行剪切處理),從截圖中再次截取我們需要的區域 codeArea.save("code_raw.png") # 保存截取的驗證碼圖片# 對圖片進行二值化處理(尷尬的是這里不做二值化處理,在下面經過放大處理后依然可以正確識別,權當研究下)out = ImageProcess("code_raw.png")# 獲取圖片大小x,y = out.size#print(x,y) # 60 20# 放大圖片(經測試這里不放大,沒法識別,原圖size有點小)resize_img = out.resize((x*2,y*2)) #print(transform.rescale(img, 2).shape) #放大為原來圖片大小的2倍#print(reimg.size) (120, 40)resize_img.save("code_erzhihua.png")result = pytesseract.image_to_string(resize_img,lang = "eng")print("result",result)# 輸入驗證碼browser.find_element_by_id("verify").send_keys(result)'''4.查詢'''# 點擊查詢按鈕browser.find_element_by_id("but_2").click()# 以下打印不出結果,待進一步研究#time.sleep(2)#elements = browser.find_element_by_class_name("form_yecx_out")#print(elements.text)# 關閉此頁面#browser.close()# 退出瀏覽器#browser.quit()# 對圖片進行簡單的二值化處理 def ImageProcess(image):image = Image.open(image)image = image.convert("L") # 灰色模式table = []threshold = 140for i in range(256):if i < threshold:table.append(0)else:table.append(1)return image.point(table,"1") # Image.point()方法操作像素數據getInfo("你的電腦號","你的身份證號")?
reference:
https://blog.csdn.net/dcba2014/article/details/78969658
http://hankerzheng.com/blog/chenge-the-size-of-pic-by-python-pil
https://blog.csdn.net/kethur/article/details/79992539
總結
以上是生活随笔為你收集整理的python中selenium(模拟登陆)+pytesseract(自动识别验证码)应用例子之查询住房公积金的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用政务网络查询单打印地址
- 下一篇: 用python输出pi的近似值_Pyth