current position:Home>Python simulated Login, selenium module, python identification graphic verification code to realize automatic login

Python simulated Login, selenium module, python identification graphic verification code to realize automatic login

2022-02-01 05:35:02 Dai mubai

「 This is my participation 11 The fourth of the yuegengwen challenge 20 God , Check out the activity details :2021 One last more challenge 」.

Preface

utilize Python Identify the graphic verification code , Automatic login . I don't say much nonsense .

Let's start happily ~

development tool

Python edition : 3.6.4

Related modules :

re;

numpy modular ;

pytesseract modular ;

selenium modular ;

As well as some Python Built in modules .

Environment building

install Python And add to environment variable ,pip Install the relevant modules required .

1.   Grayscale processing Turn the color verification code picture into a gray picture

 Grayscale processing

import cv2

image = cv2.imread('1.jpeg'0)
cv2.imwrite('1.jpg', image)
 Copy code 

2.   binarization Process the image into a black and white image , There's no interference line here , This means that we only need to deal with interference points .

 binarization

import cv2

image = cv2.imread('1.jpeg'0)
ret, image = cv2.threshold(image, 1002551)
height, width = image.shape
new_image = image[0:height, 0:150]
cv2.imwrite('1.jpg', new_image)
 Copy code 

3.   Noise reduction treatment Remove small black spots , That is, isolated black pixels .

 Noise reduction treatment

The principle of point noise reduction is to detect the adjacent black points 8 A little bit , Judge 8 The color of a dot . If it's all white dots , Then think this point is white , Make black spots turn white . Such as ⑤ point , In terms of Tian zigzag , Adjacent common 8 Regions .

 Point noise reduction principle

①②③ The point coordinates are shown in the figure below , In the same way ④⑤⑥⑦⑧⑨ Point coordinates

 Point coordinates

Noise reduction code

import cv2
import numpy as np
from PIL import Image


def inverse_color(image, col_range):
    #  Read the picture ,0 It means that the picture becomes grayscale 
    image = cv2.imread(image, 0)
    #  Image binarization ,100 Set the threshold for ,255 Is the maximum threshold ,1 Is the threshold type , The current point value is greater than the threshold value , Set to 0, Otherwise set to 255.ret yes return value abbreviation , Represents the current threshold 
    ret, image = cv2.threshold(image, 110, 255, 1)
    #  The height and width of the picture 
    height, width = image.shape
    #  Image anti color processing , reason : The above processing can only generate pictures with white characters and black background , What we need is a picture with black characters and white background 
    img2 = image.copy()
    for i in range(height):
        for j in range(width):
            img2[i, j] = (255 - image[i, j])
    img = np.array(img2)
    #  Intercept the processed picture 
    height, width = img.shape
    new_image = img[0:height, col_range[0]:col_range[1]]
    cv2.imwrite('handle_one.png', new_image)
    image = Image.open('handle_one.png')
    return image


def clear_noise(img):
    #  Image noise reduction 
    x, y = img.width, img.height
    for i in range(x):
        for j in range(y):
            if sum_9_region(img, i, j) < 2:
                #  Change pixel color , white 
                img.putpixel((i, j), 255)
    img = np.array(img)
    cv2.imwrite('handle_two.png', img)
    img = Image.open('handle_two.png')
    return img


def sum_9_region(img, x, y):
    """  Tian Zi Ge  """
    #  Gets the color value of the current pixel 
    cur_pixel = img.getpixel((x, y))
    width = img.width
    height = img.height

    if cur_pixel == 255:  #  If the current point is a white area , Then the neighborhood value is not counted 
        return 10

    if y == 0:  #  first line 
        if x == 0:  #  Top left vertex ,4 Neighborhood 
            #  Next to the center point 3 A little bit 
            sum_1 = cur_pixel + img.getpixel((x, y + 1)) + img.getpixel((x + 1, y)) + img.getpixel((x + 1, y + 1))
            return 4 - sum_1 / 255
        elif x == width - 1:  #  Top right 
            sum_2 = cur_pixel + img.getpixel((x, y + 1)) + img.getpixel((x - 1, y)) + img.getpixel((x - 1, y + 1))
            return 4 - sum_2 / 255
        else:  #  Top non vertex ,6 Neighborhood 
            sum_3 = img.getpixel((x - 1, y)) + img.getpixel((x - 1, y + 1)) + cur_pixel + img.getpixel((x, y + 1)) + img.getpixel((x + 1, y)) + img.getpixel((x + 1, y + 1))
            return 6 - sum_3 / 255

    elif y == height - 1:  #  The bottom line 
        if x == 0:  #  The lower left vertex 
            #  Next to the center point 3 A little bit 
            sum_4 = cur_pixel + img.getpixel((x + 1, y)) + img.getpixel((x + 1, y - 1)) + img.getpixel((x, y - 1))
            return 4 - sum_4 / 255
        elif x == width - 1:  #  The lower right vertex 
            sum_5 = cur_pixel + img.getpixel((x, y - 1)) + img.getpixel((x - 1, y)) + img.getpixel((x - 1, y - 1))
            return 4 - sum_5 / 255
        else:  #  The lowest non vertex ,6 Neighborhood 
            sum_6 = cur_pixel + img.getpixel((x - 1, y)) + img.getpixel((x + 1, y)) + img.getpixel((x, y - 1)) + img.getpixel((x - 1, y - 1)) + img.getpixel((x + 1, y - 1))
            return 6 - sum_6 / 255

    else:  # y Not on the border 
        if x == 0:  #  The left is not the vertex 
            sum_7 = img.getpixel((x, y - 1)) + cur_pixel + img.getpixel((x, y + 1)) + img.getpixel((x + 1, y - 1)) + img.getpixel((x + 1, y)) + img.getpixel((x + 1, y + 1))
            return 6 - sum_7 / 255
        elif x == width - 1:  #  Right non vertex 
            sum_8 = img.getpixel((x, y - 1)) + cur_pixel + img.getpixel((x, y + 1)) + img.getpixel((x - 1, y - 1)) + img.getpixel((x - 1, y)) + img.getpixel((x - 1, y + 1))
            return 6 - sum_8 / 255
        else:  #  Have 9 The field conditions are 
            sum_9 = img.getpixel((x - 1, y - 1)) + img.getpixel((x - 1, y)) + img.getpixel((x - 1, y + 1)) + img.getpixel((x, y - 1)) + cur_pixel + img.getpixel((x, y + 1)) + img.getpixel((x + 1, y - 1)) + img.getpixel((x + 1, y)) + img.getpixel((x + 1, y + 1))
            return 9 - sum_9 / 255


def main():
    img = '1.jpeg'
    img = inverse_color(img, (0, 160))
    clear_noise(img)


if __name__ == '__main__':
    main()
 Copy code 

After solving the biggest problem , The next step is to realize automatic login . use first selenium Automatically click the login button .

 The target site

Screenshot for processing , Finally, the verification code is successfully obtained .

Why is this a screenshot , The reason is that the verification code picture is changing all the time . For example, I copy this now 8863 Picture link of verification code , Open in a new tab , You will find that the verification code has changed , No 8863, It's another verification code picture . Then we get the verification code link of the current page , To get the verification code picture , This method is certainly not feasible .

By looking up relevant information , I see. Take cookies Visit the verification code link page , Can successfully solve this problem . However, because the related libraries were not imported successfully , So I gave up . When we do verification code machine learning , Then solve it .

 Login screen

Landing successful

9.jpg

copyright notice
author[Dai mubai],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010535010677.html

Random recommended