1. 程式人生 > >python+selenium識別驗證碼並登錄

python+selenium識別驗證碼並登錄

from process rep 分享 tracking refresh 文章 rom fill

python版本:3.4.3

所需要的代碼庫:PIL,selenium,tesseract

先上代碼:

#coding:utf-8
import subprocess
from PIL import Image
from PIL import ImageOps
from selenium import webdriver
import time,os,sys


def cleanImage(imagePath):
image = Image.open(imagePath) #打開圖片
image = image.point(lambda x: 0 if x<143 else 255) #處理圖片上的每個像素點,使圖片上每個點“非黑即白”
borderImage = ImageOps.expand(image,border=20,fill=‘white‘)
borderImage.save(imagePath)


def getAuthCode(driver, url="http://localhost/"):
captchaUrl = url + "common/random"
driver.get(captchaUrl)
time.sleep(0.5)
driver.save_screenshot("captcha.jpg") #截屏,並保存圖片
#urlretrieve(captchaUrl, "captcha.jpg")
time.sleep(0.5)
cleanImage("captcha.jpg")
p = subprocess.Popen(["tesseract", "captcha.jpg", "captcha"], stdout=\
subprocess.PIPE,stderr=subprocess.PIPE)
p.wait()
f = open("captcha.txt", "r")

#Clean any whitespace characters
captchaResponse = f.read().replace(" ", "").replace("\n", "")


print("Captcha solution attempt: " + captchaResponse)
if len(captchaResponse) == 4:
return captchaResponse
else:
return False


def withoutCookieLogin(url="http://org.cfu666.com/"):
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
while True:
authCode = getAuthCode(driver, url)
if authCode:
driver.back()
driver.find_element_by_xpath("//input[@id=‘orgCode‘ and @name=‘orgCode‘]").clear()
driver.find_element_by_xpath("//input[@id=‘orgCode‘ and @name=‘orgCode‘]").send_keys("orgCode")
driver.find_element_by_xpath("//input[@id=‘account‘ and @name=‘username‘]").clear()
driver.find_element_by_xpath("//input[@id=‘account‘ and @name=‘username‘]").send_keys("username")
driver.find_element_by_xpath("//input[@type=‘password‘ and @name=‘password‘]").clear()
driver.find_element_by_xpath("//input[@type=‘password‘ and @name=‘password‘]").send_keys("password")
driver.find_element_by_xpath("//input[@type=‘text‘ and @name=‘authCode‘]").send_keys(authCode)
driver.find_element_by_xpath("//button[@type=‘submit‘]").click()
try:
time.sleep(3)
driver.find_element_by_xpath("//*[@id=‘side-menu‘]/li[2]/ul/li/a").click()
return driver
except:
print("authCode Error:", authCode)
driver.refresh()
return driver


driver = withoutCookieLogin("http://localhost/")
driver.get("http://localhost/enterprise/add/")

怎麽獲取我們需要的驗證碼

在這獲取驗證碼的道路上,我掉了太多的坑,看過太多的文章,很多都是教你驗證碼的識別方法,但是沒有說明,怎麽獲取你當前需要的驗證碼圖片。

我的處理方法是:

1.先用selenium打開你需要的登錄的頁面地址url1

技術分享圖片

2.通過審核元素獲取驗證碼的地址url2(其實最簡單的是右鍵打開新頁面)

技術分享圖片

3:在url1頁面,輸入地址url2進入url2頁面,然後截屏保存驗證碼頁面

技術分享圖片

4:處理驗證碼得到驗證碼字符串。然後點擊瀏覽器後退按鈕,返回url1登錄頁面

5:輸入登錄需要的信息和驗證碼

技術分享圖片

6:點擊登錄

7:驗證登錄後的頁面,判斷是否成功,若不成功則需要重新1-7的操作。

為了保護公司的信息,這個頁面是我本地搭的服務,我在伯樂在線註冊頁面進行測試過這個驗證碼獲得方法,可以通過。(這個驗證碼的處理方法,僅限驗證碼背景是像素點,若驗證碼有橫線需額外處理。)

第一篇博文,不喜勿噴。

驗證碼處理方法參考文獻:

Web Scraping with Python.pdf

python+selenium識別驗證碼並登錄