import time from pytesseract import pytesseract from selenium import webdriver from PIL import Image if __name__ == '__main__': browser = webdriver.Chrome(executable_path='D:pythonScriptschromedriver.exe') url = 'url' browser.get(url) browser.save_screenshot('D:图片print.png') element = browser.find_element_by_xpath('//*[@id="VerifCode"]')#验证码所在的xpath路径 #获得验证码xy轴坐标 location = element.location size = element.size rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) #浏览器最大化保存图片到指定文件夹下 browser.maximize_window() im = Image.open('D:图片print.png') im = im.crop(rangle) im.save('验证码.png') shibie = Image.open('验证码.png') imgry = shibie.convert('L') table = [1] * 256 for i in range(256): table[i] = 0 if i > 115: break out = imgry.point(table, '1') out.show() box = (10, 10, 125, 45) out.crop(box) result = pytesseract.image_to_string(out) yanzheng = result.split(' ') first = yanzheng[0][0] last = len(yanzheng[-1]) if last >= 3: lastnum=yanzheng[-1][-2:] else: lastnum=yanzheng[-1][-1] jieguo = int(lastnum) - int(first) print(jieguo)
识别达到99%,另外的1%其实也是可以解决的,有时间再解决。