IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Selenium+PhantomJS(系列六:元素区域截图,OCR)

    admin发表于 2017-04-11 07:26:00
    love 0

    Selenium+PhantomJS系列教程:

    • Selenium+PhantomJS(系列一:设置User-Agent)
    • Selenium+PhantomJS(系列二:模拟登录淘宝)
    • Selenium+PhantomJS(系列三:模拟登录知乎)
    • Selenium+PhantomJS(系列四:模拟登录微博)
    • Selenium+PhantomJS(系列五:selenium的等待)
    • Selenium+PhantomJS(系列六:元素区域截图,OCR)

    Selenium+PhantomJS(系列六:元素区域截图,OCR),针对元素区域截图,然后可以利用ocr做验证码识别,或做一些图形图像分析,等等。

    import base64
    import sys
    import pyocr
    from StringIO import StringIO
    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from PIL import Image
    
    driver = webdriver.Remote(
        command_executor='http://10.10.10.140:8910',
        desired_capabilities=DesiredCapabilities.PHANTOMJS)
    
    driver.get('目标网址')
    
    
    # 获取元素位置
    element = driver.find_element_by_class_name('conttxt')
    location = element.location
    size = element.size
    
    # 计算出元素位置图像坐标
    img = Image.open(StringIO(base64.decodestring(driver.get_screenshot_as_base64())))
    driver.quit()
    left = location['x']
    top = location['y']
    right = location['x'] + size['width']
    bottom = location['y'] + size['height']
    img = img.crop((int(left), int(top), int(right), int(bottom)))
    # img.save('screenshot.png') 是否保存图像
    
    
    # 利用pyocr库 推荐引擎tesseract进行图像识别
    tools = pyocr.get_available_tools()[:]
    print tools[0].image_to_string(img,lang='chi_sim')

     



沪ICP备19023445号-2号
友情链接