# -*- coding:utf-8 -*-
#__author__ = "林深见鹿,海蓝见鲸"
# date:2020-12-09
# 识别图片验证码
from selenium import webdriver
import time
from PIL import Image
import os
# import tesseract
#解析图片验证码
import pytesseract
def Ver_code():
driver = webdriver.Chrome(executable_path="C:Downloadchromedriver.exe")
_url = "https://passport.51zhongzi.com/login"
driver.get(_url)
driver.maximize_window()
driver.fullscreen_window()
time.sleep(1)
driver.find_element_by_name("az_username").send_keys(15000000000)
driver.find_element_by_name("az_password").send_keys(111111)
# 获取当前时间戳
times = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
print("获取当前时间戳:",times)
# 获取当前工作目录
path = "C:\Users\jiaow\Desktop\"
img_path = path + times + ".png"
print("验证码图片存放位置:",img_path)
# 保存验证码图片
driver.save_screenshot(img_path)
# 获取图片的坐标元素
code_element = driver.find_element_by_class_name("verifi-code")
# 打印出 图片的坐标(.location方法)#(x:133,y:465)
print("图片坐标:",code_element.location)
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width'] + left
height = code_element.size['height'] + top
# 打开保存的图片
im = Image.open(img_path)
# im = Image.convert()
# 剪切图片尺寸
img = im.crop((left, top, right, height))
# 重新保存图片
img.save(img_path)
image = Image.open(img_path)
image1 = Image.open("C:\Users\jiaow\Desktop\1.jpg") # 识别图片中的验证码并打印
time.sleep(2)
value = str(pytesseract.image_to_string(image))
# value = str(pytesseract.image_to_string(image1))
time.sleep(2)
print("验证码:",value)
if value is None:
print("验证码为空")
time.sleep(2)
driver.find_element_by_name("az_code").send_keys(value)
driver.find_element_by_class_name("login-btn").click()
driver.quit()
if __name__ == '__main__':
Ver_code()