问答

想用python自动识别验证码并输入运行报错

作者:admin 2021-04-19 我要评论

!C:/Python27 coding= utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys from pytesseract import * from PIL import Im...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

!C:/Python27

coding= utf-8

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from pytesseract import *
from PIL import Image, ImageEnhance, ImageFilter
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import os, time
def before():

driver.get(src)
time.sleep(1)
driver.maximize_window()  # 浏览器全屏显示

print('n浏览器全屏显示 ...')
def Convertimg():

imglocation = ("//*[@id='imgCode']")
# 下载验证码图片保存到本地

driver.save_screenshot('D:pythonScriptCodeimagescode.png')

# 打开本地图

im = Image.open('D:pythonScriptCodeimagescode.png')

left = driver.find_element_by_xpath(imglocation).location['x']
top = driver.find_element_by_xpath(imglocation).location['y']
right = driver.find_element_by_xpath(imglocation).location['x'] + driver.find_element_by_xpath(imglocation).size[
    'width']
bottom = driver.find_element_by_xpath(imglocation).location['y'] + driver.find_element_by_xpath(imglocation).size[
    'height']
im = im.crop((left, top, right, bottom))
im.save('D:pythonScriptCodeimagesscreenshot.png')
print

u"n保存验证码图片完成"
# 移除截屏的图片
os.remove('D:pythonScriptCodeimagescode.png')

print

u"n删除截屏图片完成"
# 处理验证码图片
src = ('D:pythonScriptCodeimagesscreenshot.png')

# 调用裁剪图片方法

Cutedge(src)

# 移除截屏的图片

os.remove('D:pythonScriptCodeimagesscreenshot.png')

# 灰化图片处理

im = Image.open('D:pythonScriptCodeimagesCutedgeImage.png')

imgry = im.convert('L')
# 二值化处理

threshold = 100
table = []

for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
out = imgry.point(table, '1')
out.save('D:pythonScriptCodeimagesrgb.png')
# vcode = pytesseract.image_to_string(out)

# print (vcode)
txtcode = image_to_string(out)

print

u"n识别出验证码文字为:", image_to_string(out)

print

len(txtcode.strip())

print

if len(txtcode.strip()) == 4:

    print

u"长度相等"
else:

    print

u"长度不相等,退出"
driver.quit()

# 输入用户名和密码

driver.find_element_by_id("email").send_keys("123456789")

driver.find_element_by_id("pwd").send_keys("123456789")
time.sleep(2)
# 对文本框输入验证码值

driver.find_element_by_id("verifyCode").send_keys(txtcode.strip())

time.sleep(3)
# 点击登录

driver.find_element_by_xpath("//*[@id='denglu']").click()

# driver.find_element_by_class_name('loginbtn').click()

time.sleep(7)
针对有黑色边框的验证码图片的裁剪边缘
def Cutedge(src):

# 设置要裁剪的区域

im = Image.open(src)

w, h = im.size
print

u"n验证码原图宽、高尺寸为:", w, h

box = (2, 2, 110, 30)
im.crop(box).save('D:pythonScriptCodeimagesCutedgeImage.png')
print

u"n保存裁剪的图片 CutedgeImage.png"
src = ("https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/c...x")
driver = webdriver.Chrome() # Firefox()#
def method_2(src):

before()
# 调用图片裁剪方法

Convertimg()
def clickInput():

driver.find_element_by_id("inputButton").click()
print

"nInput Click Finish"
def clickOutput():

print

u"n开始执行点击事件"
# 开始执行点击事件 inputButton
driver.find_element_by_id("outputButton").click()

time.sleep(2)
print(u'n开始执行任务,执行间隔时间为10分钟 ...')
for i in range(1, 4):
    ISOTIMEFORMAT = "%Y-%m-%d %X"

strTime = time.strftime(ISOTIMEFORMAT, time.localtime())

    driver.refresh()
    print

u"n正在执行第 ", i, "次...", strTime

    time.sleep(5)
    driver.find_element_by_id("outputButton").click()
    time.sleep(30)
    print

# 刷新浏览器
print
u"n刷新当前页面 ..."
driver.refresh()

    print(u'n等待间隔时间为9分钟 ...')
    time.sleep(505)
    print

u"n已执行完第 ", i, u"次,", u"已等待", i * 10, u"分钟"
print(u'n已执行完成...At The End OF,' + strTime)

driver.quit()

def isPass():

try:
    # driver.find_element_by_id("username").is_displayed() == True

driver.find_element_by_id('status').text == (u"验证码不正确!")

    print(u"n****校验提示信息_验证码输入不正确****")
    driver.quit()
    print(u"n关闭浏览器,执行外层循环...")
except Exception:
    print(u"n****校验提示信息_验证码输入正确****")
    clickOutput()  # ------  click Output

method_2(src) # 进入工作页面
isPass()
clickInput() #------ click Input
clickOutput() #------ click Output
for i in range(1, 6):

driver = webdriver.Chrome()
src = ("https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx")
method_2(src)
isPass()

clickOutput()

报错:
图片.png
图片.png
图片.png
图片.png
图片.png
请问如何解决?

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • nginx响应速度很慢

    nginx响应速度很慢

  • 点击选中的多选框,会在已选那一栏显示

    点击选中的多选框,会在已选那一栏显示

  • PHP 多态的理解

    PHP 多态的理解

  • 关于C语言中static的问题

    关于C语言中static的问题

腾讯云代理商
海外云服务器