PIL模块的安装html
pytesser 模块的安装:python
下载后获得 “pytesser_v0.0.1.zip”,是一个压缩文件,使用方法: web
一、在 “D:\For_Software\others\Python_PyDev\Python\Lib\site-packages” 路径下新建一个文件夹,命名 “pytesser” 。把 “pytesser_v0.0.1.zip” 里的文件解压到该目录:函数
二、将 “pytesser.py” 更名为 “__init__.py”。ui
三、打开 “__init__.py” 文件,将 “tesseract_exe_name” 变量的值改成 “‘D:/For_Software/others/Python_PyDev/python/Lib/site-packages/pytesser/tesseract’”(原值为 “‘tesseract’”)。url
四、pytesser 模块依赖于 PIL 模块,若是是按照上面的方法安装 PIL 的话,须要把 “init.py” 文件里的 “import Image” 改为 “from PIL import Image” 。 spa
http://blog.csdn.net/mrlevo520/article/details/51901579.net
http://www.cnblogs.com/wolaiye320/p/5649462.htmlcode
from selenium import webdriverhtm
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import unittest, time, re
from PIL import ImageGrab
from PIL import Image,ImageEnhance
from pytesser import *
import pytesseract
import os
class Cjlr(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
self.base_url = "http://182.92.131.35:8088/"
def test_cjlr(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_xpath(".//*[@id='mini-3']/span").click()
time.sleep(5)
#获取验证码
driver.save_screenshot('E:\\OCR\\aa.png') # 截取当前网页,该网页有咱们须要的验证码
imgelement = driver.find_element_by_id('img_code') # 定位验证码
location = imgelement.location # 获取验证码x,y轴坐标
size = imgelement.size # 获取验证码的长宽
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成咱们须要截取的位置坐标
i = Image.open("E:\\OCR\\aa.png") # 打开截图
frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取咱们须要的区域
frame4.save('E:\\OCR\\ac.png')
qq = Image.open('E:\\OCR\\ac.png')
imgry = qq.convert('L') # 图像增强,二值化
sharpness = ImageEnhance.Contrast(imgry) # 对比度加强
sharp_img = sharpness.enhance(2.0)
sharp_img.save("E:\\OCR\\ac.png")
code = image_file_to_string("E:\\OCR\\ac.png") # code即为识别出的图片数字str类型
print code
#登陆
driver.find_element_by_id('tel_num').send_keys('18336091139')
driver.find_element_by_id('pwd').send_keys('123456')
time.sleep(10)
driver.find_element_by_id('randCode').send_keys(code)
driver.find_element_by_id("teacher_login").click()