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
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 .
import cv2
image = cv2.imread('1.jpeg', 0)
ret, image = cv2.threshold(image, 100, 255, 1)
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 .
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 .
①②③ The point coordinates are shown in the figure below , In the same way ④⑤⑥⑦⑧⑨ 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 .
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 .
Landing successful
copyright notice
author[Dai mubai],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010535010677.html
The sidebar is recommended
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
guess what you like
-
Daily python, Chapter 18, class
-
"I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
-
Django view
-
Python implements filtering emoticons in text
-
When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
-
Python crawler - get fund change information
-
Highlight actor using Python VTK
-
Python crawler actual combat: crawling southern weekend news articles
-
leetcode 406. Queue Reconstruction by Height(python)
-
leetcode 1043. Partition Array for Maximum Sum (python)
Random recommended
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
- Python series tutorial 127 -- Reference vs copy
- Control flow in Python: break and continue
- Teach you how to extract tables in PDF with Python
- leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(python)
- leetcode 1338. Reduce Array Size to The Half(python)
- Object oriented and exception handling in Python
- How to configure load balancing for Django service
- How to embed Python in go
- Python Matplotlib drawing graphics
- Python object-oriented programming 05: concluding summary of classes and objects
- Python from 0 to 1 (day 14) - Python conditional judgment 1
- Several very interesting modules in Python
- How IOS developers learn Python Programming 15 - object oriented programming 1
- Daily python, Chapter 20, exception handling
- Understand the basis of Python collaboration in a few minutes
- [centos7] how to install and use Python under Linux
- leetcode 1130. Minimum Cost Tree From Leaf Values(python)
- leetcode 1433. Check If a String Can Break Another String(python)
- Python Matplotlib drawing 3D graphics
- Talk about deep and shallow copying in Python
- Python crawler series - network requests
- Python thread 01 understanding thread
- Analysis of earthquake distribution in the past 10 years with Python~
- You need to master these before learning Python crawlers
- After the old friend (R & D post) was laid off, I wanted to join the snack bar. I collected some data in Python. It's more or less a intention
- Python uses redis
- Python crawler - ETF fund acquisition
- Detailed tutorial on Python operation Tencent object storage (COS)
- [Python] comparison of list, tuple, array and bidirectional queue methods
- Go Python 3 usage and pit Prevention Guide