selenium+pytesseract 验证码登录【半成品,有待改进】

import time
from selenium import webdriver
from PIL import Image, ImageEnhance
import pytesseract  
driver = webdriver.Firefox()
driver.get('http://*****')

#让页面加载
time.sleep(2)

#找到第一个iframe
iframe =driver.find_element_by_xpath("//html/body/table/tbody/tr[3]/td[2]/div[1]/table/tbody/tr/td/iframe")

x=iframe.location['x']
y=iframe.location['y']
#print(x,y)

#切换到iframe
driver.switch_to_frame(iframe)

inputText=driver.find_element_by_id('txt_sdertfgsadscxcadsads')
inputText.click()
time.sleep(2)
cpatcha=driver.find_element_by_id('imgCode')

#截取整个页面
driver.save_screenshot('D://page.png')


#获取验证码图片的x,y坐标,以及自身宽度和高度
left = x+cpatcha.location['x']
top =y+cpatcha.location['y']
right =x+cpatcha.location['x'] + cpatcha.size['width']
bottom = y+cpatcha.location['y'] + cpatcha.size['height']

# print left,top,right,bottom
#截取验证码图片
img = Image.open('D://page.png')
img = img.crop((left,top,right,bottom))
#保存到本地
img.save('D://captcha.png')

#print(driver.page_source)

im = Image.open('D://captcha.png')
im = im.convert('L')#图像加强,二值化
sharpness =ImageEnhance.Contrast(im)#对比度增强
sharp_img = sharpness.enhance(2.0)
sharp_img.save("D://newVerifyCode.png")


print( 'abc',pytesseract.image_to_string(sharp_img))










猜你喜欢

转载自blog.csdn.net/qq_34525938/article/details/81028469
今日推荐