识别诺诺金服页面的验证码,并自动登录到后台。

 #导包
from selenium import webdriver
from PIL import Image, ImageDraw
from time import sleep
import pytesseract
#定义浏览器句柄,赋值url为诺诺金服后台地址
driver = webdriver.Chrome()
driver.implicitly_wait(10) #隐式等待10s
url = 'https://gl.axnjf.com/Contents/global/login/login.jsp'
#窗口最大化
driver.maximize_window()
driver.get(url)
# 诺诺金服后台页面截取验证码,代码如下:
driver.save_screenshot('E:/test_nuonuojinfu/step_01.png')
#定位验证码
imgelement = driver.find_element_by_xpath('//*[@id="img_validator"]')
#获取验证码x,y轴坐标
location = imgelement.location
#获取验证码的长宽
size = imgelement.size
#写成我们需要截取的位置坐标
rangle = (int(location['x']), int(location['y']), int(location['x']+size['width']), int(location['y']+size['height']))
i = Image.open('E:/test_nuonuojinfu/step_01.png')
#二次截图
frame4 = i.crop(rangle)
#二次截图后命名
frame4.save('E:/test_nuonuojinfu/step_02.png')
print('二次截图成功!') #对截取后的验证码二值化,代码如下:
# 二值数组
t2val = {}
def twoValue(image, G):
for y in range(0, image.size[1]):
for x in range(0, image.size[0]):
g = image.getpixel((x, y))
if g > G:
t2val[(x, y)] = 1
else:
t2val[(x, y)] = 0 def saveImage(filename, size):
image = Image.new("", size)
draw = ImageDraw.Draw(image)
for x in range(0, size[0]):
for y in range(0, size[1]):
draw.point((x, y), t2val[(x, y)])
image.save(filename) path = 'E:/test_nuonuojinfu/step_02.png'
image = Image.open(path)
image = image.convert('L')
twoValue(image, 125)
path1 = 'E:/test_nuonuojinfu/step_03.jpg'
saveImage(path1, image.size)
print("返回信息:验证码到手,增强成功,春哥附体,定能识别!")
#将处理后的验证码用Tesseract识别,代码如下:
#添加pytesseract参数变量
pytesseract.pytesseract.tesseract_cmd= "C:\Program Files (x86)\Tesseract-OCR/tesseract.exe"
tessdata_dir_config = '--tessdata-dir "C:\Program Files (x86)\Tesseract-OCR/tessdata" '
#打开需要识别的验证码图片
image1 = Image.open("E:/test_nuonuojinfu/step_03.jpg")
#识别验证码,赋值变量为code
print('开始识别二维码,请等待...')
code = pytesseract.image_to_string(image1)
#打印识别后的验证码
print('验证码:' + str(code))
#拿到验证码后,进行登录操作,代码如下:
driver.find_element_by_xpath('//*[@id="username"]').send_keys("nbiaabc") #输入账号
driver.find_element_by_xpath('//*[@id="password"]').send_keys("fBmhicc") #输入密码
#输入拿到的验证码
driver.find_element_by_xpath('//*[@id="validator"]').send_keys(code) #输入验证码
# 点击登录按钮
driver.find_element_by_xpath('//*[@id="button"]').click()
sleep(1)
driver.quit()

yzmsb_test.py的更多相关文章

  1. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  2. Python导入其他文件中的.py文件 即模块

    import sys sys.path.append("路径") import .py文件

  3. import renumber.py in pymol

    cp renumber.py /usr/local/lib/python2.7/dist-packages/pymol import renumber or run /path/to/renumber ...

  4. python gettitle.py

    #!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...

  5. 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!

    情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...

  6. caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例

    caffe机器学习环境搭建及python接口编译参见我的上一篇博客:机器学习caffe环境搭建--redhat7.1和caffe的python接口编译 1.运行caffe图片分类器python接口 还 ...

  7. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  8. MySqlNDB使用自带的ndb_setup.py安装集群

    在用Mysql做集群时,使用Mysql的NDB版本更易于集群的扩展,稳定和数据的实时性. 我们可以使用Mysql自带的工具进行集群安装与管理:ndb_setup.py.位于Mysql的安装目录bin下 ...

  9. 将做好的py文件打包成模块,供别人安装调用

    现在要将写完的3个py文件,打包. 步骤: 1.新建一个文件夹setup(名字随便取),在setup文件夹下,再新建一个文件夹financeapi. 2.将上面4个py文件拷贝至financeapi文 ...

随机推荐

  1. ASP.NET MVC升级到ASP.NET Core MVC踩坑小结

    写在前面 ASP.NET Core是微软新推出的支持跨平台.高性能.开源的开发框架,它的优势不必多说,因为已经说得太多了.当然,现在依然有着数量庞大的系统运行于.NET Framework上,由于有大 ...

  2. Spark作业提交至Yarn上执行的 一个异常

    (1)控制台Yarn(Cluster模式)打印的异常日志: client token: N/A         diagnostics: Application application_1584359 ...

  3. Nuget多项目批量打包上传服务器的简明教程

    本篇不会介绍Nuget是什么,如何打包上传Nuget包,怎么搭建私有Nuget服务器.这些问题园子里都有相应的文章分享,这里不做过多阐述.另外本文假设你已经下载了Nuget.exe,并且已经设置好了环 ...

  4. 使用TensorFlow v2.0构建卷积神经网络

    使用TensorFlow v2.0构建卷积神经网络. 这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制. CNN 概述 MNIST 数据集概述 此示例使用手写数字的MNIST数 ...

  5. eureka和zookeeper注册中心的区别

    ookeeper与Eureka区别 CPA理论:一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡. ...

  6. Spring ioc xml 实例化bean 自己实现

    public class DefClassPathXmlApplicationContext { private String xmlPath; public DefClassPathXmlAppli ...

  7. JVM tomcat 性能调优

    1,新建web 测试项目并且发布到Tomcat,访问路径:http://127.0.0.1:8080/JvmWeb/index @WebServlet("/index") publ ...

  8. iOS 图片加载和处理

    一.图片显示 图片的显示分为三步:加载.解码.渲染.解码和渲染是由 UIKit 进行,通常我们操作的只有加载. 以 UIImageView 为例.当其显示在屏幕上时,需要 UIImage 作为数据源. ...

  9. [POJ2533]Longest Ordered Subsequence<dp>

    题目链接:http://poj.org/problem?id=2533 描述: A numeric sequence of ai is ordered if a1 < a2 < ... & ...

  10. 读者来信 | 刚搭完HBase集群,Phoenix一启动,HBase就全崩了,是什么原因?(已解决)

    前言:之前有朋友加好友与我探讨一些问题,我觉得这些问题倒挺有价值的:于是就想在本公众号开设一个问答专栏,方便技术交流与分享,专栏名就定为:<读者来信>.如遇到本人能力有限难以解决的问题,我 ...