python+selenium识别验证码并登录的示例代码


Posted in Python onDecember 21, 2017

由于工作需要,登录网站需要用到验证码。最初是研究过验证码识别的,但是总是不能获取到我需要的那个验证码。直到这周五,才想起这事来,昨天顺利的解决了。

下面正题:

python版本:3.4.3

所需要的代码库:PIL,selenium,tesseract

先上代码:

#coding:utf-8
import subprocess
from PIL import Image
from PIL import ImageOps
from selenium import webdriver
import time,os,sys
def cleanImage(imagePath):
  image = Image.open(imagePath)  #打开图片
  image = image.point(lambda x: 0 if x<143 else 255) #处理图片上的每个像素点,使图片上每个点“非黑即白”
  borderImage = ImageOps.expand(image,border=20,fill='white')
  borderImage.save(imagePath)

def getAuthCode(driver, url="http://localhost/"):
  captchaUrl = url + "common/random"
  driver.get(captchaUrl) 
  time.sleep(0.5)
  driver.save_screenshot("captcha.jpg")  #截屏,并保存图片
  #urlretrieve(captchaUrl, "captcha.jpg")
  time.sleep(0.5)
  cleanImage("captcha.jpg")
  p = subprocess.Popen(["tesseract", "captcha.jpg", "captcha"], stdout=\
             subprocess.PIPE,stderr=subprocess.PIPE)
  p.wait()
  f = open("captcha.txt", "r")
  
  #Clean any whitespace characters
  captchaResponse = f.read().replace(" ", "").replace("\n", "")
  print("Captcha solution attempt: " + captchaResponse)
  if len(captchaResponse) == 4:
    return captchaResponse
  else:
    return False

def withoutCookieLogin(url="http://org.cfu666.com/"):
  driver = webdriver.Chrome()
  driver.maximize_window()
  driver.get(url)
  while True:   
    authCode = getAuthCode(driver, url)
    if authCode:
      driver.back()
      driver.find_element_by_xpath("//input[@id='orgCode' and @name='orgCode']").clear()
      driver.find_element_by_xpath("//input[@id='orgCode' and @name='orgCode']").send_keys("orgCode")
      driver.find_element_by_xpath("//input[@id='account' and @name='username']").clear()
      driver.find_element_by_xpath("//input[@id='account' and @name='username']").send_keys("username")
      driver.find_element_by_xpath("//input[@type='password' and @name='password']").clear()
      driver.find_element_by_xpath("//input[@type='password' and @name='password']").send_keys("password")       
      driver.find_element_by_xpath("//input[@type='text' and @name='authCode']").send_keys(authCode)
      driver.find_element_by_xpath("//button[@type='submit']").click()
      try:
        time.sleep(3)
        driver.find_element_by_xpath("//*[@id='side-menu']/li[2]/ul/li/a").click()
        return driver
      except:
        print("authCode Error:", authCode)
        driver.refresh()
  return driver
driver = withoutCookieLogin("http://localhost/")
driver.get("http://localhost/enterprise/add/")

怎么获取我们需要的验证码

在这获取验证码的道路上,我掉了太多的坑,看过太多的文章,很多都是教你验证码的识别方法,但是没有说明,怎么获取你当前需要的验证码图片。

我的处理方法是:

1.先用selenium打开你需要的登录的页面地址url1

python+selenium识别验证码并登录的示例代码

2.通过审核元素获取验证码的地址url2(其实最简单的是右键打开新页面)

python+selenium识别验证码并登录的示例代码

3:在url1页面,输入地址url2进入url2页面,然后截屏保存验证码页面

python+selenium识别验证码并登录的示例代码

4:处理验证码得到验证码字符串。然后点击浏览器后退按钮,返回url1登录页面

5:输入登录需要的信息和验证码

python+selenium识别验证码并登录的示例代码

6:点击登录

7:验证登录后的页面,判断是否成功,若不成功则需要重新1-7的操作。

为了保护公司的信息,这个页面是我本地搭的服务,我在伯乐在线注册页面进行测试过这个验证码获得方法,可以通过。(这个验证码的处理方法,仅限验证码背景是像素点,若验证码有横线需额外处理。)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中as用法实例分析
Apr 30 Python
Python 列表排序方法reverse、sort、sorted详解
Jan 22 Python
python去除文件中空格、Tab及回车的方法
Apr 12 Python
关于python的bottle框架跨域请求报错问题的处理方法
Mar 19 Python
基于python的字节编译详解
Sep 20 Python
python实战教程之自动扫雷
Jul 13 Python
python保存二维数组到txt文件中的方法
Nov 15 Python
python截取两个单词之间的内容方法
Dec 25 Python
Python简单过滤字母和数字的方法小结
Jan 09 Python
Python3.6+Django2.0以上 xadmin站点的配置和使用教程图解
Jun 04 Python
python 两种方法删除空文件夹
Sep 29 Python
Python中三种花式打印的示例详解
Mar 19 Python
python实现随机森林random forest的原理及方法
Dec 21 #Python
python编写分类决策树的代码
Dec 21 #Python
Python基于PyGraphics包实现图片截取功能的方法
Dec 21 #Python
用Python写王者荣耀刷金币脚本
Dec 21 #Python
python使用Apriori算法进行关联性解析
Dec 21 #Python
python实现kMeans算法
Dec 21 #Python
利用Tkinter(python3.6)实现一个简单计算器
Dec 21 #Python
You might like
php+mysqli使用预处理技术进行数据库查询的方法
2015/01/28 PHP
yii2超好用的日期组件和时间组件
2016/05/05 PHP
微信公众平台开发教程④ ThinkPHP框架下微信支付功能图文详解
2019/04/10 PHP
PHP 加密 Password Hashing API基础知识点
2020/03/02 PHP
JavaScript面向对象之体会[总结]
2008/11/13 Javascript
javascript小数计算出现近似值的解决办法
2010/02/06 Javascript
微信小程序 页面之间传参实例详解
2017/01/13 Javascript
详解jquery选择器的原理
2017/08/01 jQuery
JavaScript实现数值自动增加动画
2017/12/28 Javascript
javaScript动态添加Li元素的实例
2018/02/24 Javascript
vue中使用elementUI组件手动上传图片功能
2019/12/13 Javascript
[02:38]2018DOTA2亚洲邀请赛赛前采访-VGJ.T
2018/04/03 DOTA
[22:07]DOTA2-DPC中国联赛 正赛 iG vs Magma 选手采访
2021/03/11 DOTA
Python2.5/2.6实用教程 入门基础篇
2009/11/29 Python
Python装饰器基础详解
2016/03/09 Python
Python面向对象程序设计类的多态用法详解
2019/04/12 Python
使用python搭建服务器并实现Android端与之通信的方法
2019/06/28 Python
python requests证书问题解决
2019/09/05 Python
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
2020/01/20 Python
pygame实现飞机大战
2020/03/11 Python
python实现最速下降法
2020/03/24 Python
Python实现初始化不同的变量类型为空值
2020/06/02 Python
python安装后的目录在哪里
2020/06/21 Python
Python openpyxl模块实现excel读写操作
2020/06/30 Python
英国豪华文具和皮具配件经典老品牌:Smythson(斯迈森)
2018/04/19 全球购物
DTD的含义以及作用
2014/01/26 面试题
学年自我鉴定范文
2013/10/01 职场文书
初一生物教学反思
2014/01/18 职场文书
工作表现评语
2014/01/19 职场文书
公司踏青活动方案
2014/08/16 职场文书
研修心得体会
2014/09/04 职场文书
终止或解除劳动合同及劳动关系的证明书
2014/10/06 职场文书
三严三实心得体会范文
2014/10/13 职场文书
2015年上半年信访工作总结
2015/03/30 职场文书
务工证明怎么写
2015/06/18 职场文书
golang的文件创建及读写操作
2022/04/14 Golang