Python 自动化之验证码识别
之前公司的验证码比较简单,可以采取直接破解的方式进行登录
部分代码如下:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Selectimport unittest,time,re,sys
from PIL import Image
import pytesseract reload(sys)
sys.setdefaultencoding('utf-8') class Ypt(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://*********.com"
self.verificationErrors = []
self.accept_next_alert = True def get_streen(self):
driver = self.driver
driver.save_screenshot('D://aa.png') #截取当前网页,该网页有我们需要的验证码
imgelement = driver.find_element_by_xpath('//*[@id="id_checkCode"]') #定位验证码
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("D://aa.png") #打开截图
frame4=i.crop(rangle) #使用Image的crop函数,从截图中再次截取我们需要的区域
frame4.save('D://frame4.png')
img = Image.open('D://frame4.png')
print img.load()
aa = .image_to_string(img)
print u"识别的验证码为:"
print aa
if aa == "": #如果识别为空,则再一次识别
driver.find_element_by_xpath('//*[@id="id_checkCode"]').click()
self.get_streen()
return aa def test_ypt(self):
now_time = open("yuheng.txt","a")
driver = self.driver
driver.maximize_window()
driver.get(self.base_url + "/userLoginOut.screen")
driver.find_element_by_id("j_username").clear()
driver.find_element_by_id("j_username").send_keys("username")
driver.find_element_by_id("j_password").clear()
driver.find_element_by_id("j_password").send_keys("password")
driver.find_element_by_id("id_imgCode").clear()
driver.find_element_by_id("id_imgCode").send_keys(self.get_streen())
driver.find_element_by_id("btn-login").click()
driver.find_element_by_id("C5A000005").click()
driver.find_element_by_link_text(u"概况数据").click()
self.assertEqual(u"**************", driver.title)
time.sleep(8)def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return Trueif __name__ == "__main__":
unittest.main()
Python 自动化之验证码识别的更多相关文章
- Python 代码实现验证码识别
Python 代码实现验证码识别 测试开发社区 1周前 源 / j_hao104 一.探讨 识别图形验证码可以说是做爬虫的必修课,涉及到计算机图形学,机器学习,机器视觉,人工智能等等高深领域…… ...
- 字符识别Python实现 图片验证码识别
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- python自动化实现验证码登录过程
(自动化实现验证码登录,这里内容是入坑后,整合了几个文档的内容)|以下模块是使用时需要用到的首先:安装pillow库,它的作用是对图片进行简单的处理,在pytharm中使用pip install pi ...
- 基于SVM的python简单实现验证码识别
验证码识别是一个适合入门机器学习的项目,之前用knn 做过一个很简单的,这次用svm来实现.svm直接用了开源的库libsvm.验证码选的比较简单,代码也写得略乱,大家看看就好. 1. 爬取验证码图片 ...
- Python实现各类验证码识别
项目地址: https://github.com/kerlomz/captcha_trainer 编译版下载地址: https://github.com/kerlomz/captcha_trainer ...
- Python中机器学习-验证码识别-粗略总结
#验证码识别# 解决办法:将验证码切割成单个字符训练 遇到问题:验证码字符大小不一或重叠 对上述问题的解决:通过CNN(卷积神经网络)直接就是端到端不分割的识别方式 处理验证码:将图片二值化 输入验证 ...
- Python图像处理之验证码识别
在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract ...
- python实现中文验证码识别方法(亲测通过)
验证码截图如下: # coding:utf-8from PIL import Image,ImageEnhanceimport pytesseract#上面都是导包,只需要下面这一行就能实现图片文字识 ...
- python tesseract-ocr 基础验证码识别功能(Windows)
一.环境 windows 7 x64 Python 3 + 二.安装 1.tesseract-ocr安装 http://digi.bib.uni-mannheim.de/tesseract/ 2.py ...
随机推荐
- MySQL集群---②Windows平台搭建MySQL CLUSTER集群
原文:http://blog.csdn.net/mazhaojuan/article/details/42211857 本文将通过两台电脑来简单介绍一下Windows平台如何搭建MySQL集群. My ...
- How to create an IPA (Xcode 5)
This tutorial will walk you through the easiest way to generate an IPA using Xcode 5. We will be usi ...
- 计算GPS两点间的距离[单位为:米]
/** * 计算GPS两点间的距离[单位为:米] * @param center GPS当前数据(LonLat对象表示,LonLat.lon表示经度,LonLat.lat表示纬度) ...
- jconsole使用记录
jconsole/JVisualVM连接linux服务器查看JVM使用情况 现需要在本地电脑上查看服务器的tomcat的整体的运行状态,使用jconsole工具. JMX配置 拷贝$JAVA_HOME ...
- VirtualBox下Linux(centos)扩展磁盘空间
最近在Linux里做文件合并,做分词,磁盘空间不够,把扩展磁盘空间方法记录一下. 1.在VirtualBox安装路径下(例如C:\Program Files\Oracle\VirtualBox> ...
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下驱动器试运行提示过速度保护怎么办
在试运行的时候,取消勾选自动设定,然后可以自己设置过速度等级设置和过载等级设置 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaoh ...
- Android中Activity的生命周期图
- Step 2---有关Github的几个问题
1.取得项目的Git仓库的方式 第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库. 要对现有的某个项目开始用 Git 管理,只需到此项目所在的目录,执行: $ git init 初始化 ...
- HBase数据同步到ElasticSearch的方案
ElasticSearch的River机制 ElasticSearch自身提供了一个River机制,用于同步数据. 这里能够找到官方眼下推荐的River: http://www.elasticsear ...
- EFFECTIVE JAVA 第一天 静态工厂方法
静态工厂方法:(这里指的是就是普通static方法),类可以通过静态工厂方法提供给它的客户端,而不是通过构造器.提供静态工厂方法而不是公有构造器,这样做有几大优势. 在类的实现中使用了API的类被称为 ...