Processing of picture verification code by Python+Requests

Requests' processing of image verification codes

The login interface on the web side often has a picture verification code input, and the picture verification code is random every time you log in; when you log in through the request interface, you need to identify the picture verification code to identify the fields in the picture, and then Used in the login interface;

Recognition method of picture verification code through request (picture with noise)

1. Identify the image verification code by installing the OcrServer tool locally

Baidu download ocrserver tool

As shown in the figure below: double-click OcrServer.exe after decompression; then the IP and port of the service will be displayed in the lower right corner of the computer

2. Write a script through python, and cooperate with the OcrServer tool to identify the value of the image verification code

Preconditions: Import the required plugins

import base64
import request

1. The interface to send the picture verification code, return the response of the picture and save it to the specified folder

# Get the verification code image and save it as 123.png 
response = requests.get('Get the URL address of the verification code image') 
img = response.content 
with open('../sample/123.png','wb ') as f: 
    f.write(img)

2. After opening and reading the picture, encode the picture through base64

# After reading the image, encode the image via base64 
png = open('123.png','rb') 
res = png.read() 
s = base64.b64encode(res) 
png.close() 
# print(s .decode('ascii'))

3. After opening the OcrServer.exe plug-in locally, send the encoded picture to the specified url address, and the return value is in json format

# After opening the OcrServer.exe plug-in locally, send the encoded picture to the specified url address, and the return value is in json format: {"code":"The value of the verification code picture"} response = requests.post(url=" 
http ://127.0.0.1:12349",data=s) 
code_num = response.json() 
print(code_num['code'])

4. View the value of the identified verification code image

The obtained verification code picture is as follows

 After the code is executed, the output result is as follows

 The whole code is shown as an example in the figure below: it can be written as an encapsulation class to call

import base64 
import requests 

# Get the verification code picture and save it as 123.png 
response = requests.get('Get the url address of the picture verification code') 
img = response.content 
with open('../sample/123.png ','wb') as f: 
    f.write(img) 

# After reading the picture, encode the picture through base64 
png = open('123.png','rb') 
res = png.read() 
s = base64.b64encode(res) 
png.close() 
# print(s.decode('ascii')) 

# After opening the OcrServer.exe plug-in locally, send the encoded picture to the specified url address, and the return value is in json format { "code": "The value of the verification code image"} 
response = requests.post(url="http://127.0.0.1:12349",data=s) 
code_num = response.json() 
print(code_num['code' ])

By writing python code, importing third-party libraries (), and identifying image verification codes (images without noise)

 A picture without noise; as shown below:

Pure numbers: The image name of the code below is replaced by 666.png

Pure Chinese: The picture name of the following code is replaced by 999.png

 1. First download the Pillow library and the pytesseract library to identify the image verification code

pip install Pillow
pip install pytesseract

2. Import the third-party library, and then write the following code to identify the image verification code;

Sample code: The following is an example of a pure digital image verification code

from PIL import Image 
import pytesseract 

# pytesseract and PIL can only successfully recognize image verification codes without noise 
path = '666.png' 
captcha = Image.open(path) 
result = pytesseract.image_to_string(captcha,lang="chi_sim") 
print(result)

Execution result: recognition succeeded

 Sample code: The following is an example of a pure Chinese image verification code

from PIL import Image 
import pytesseract 

# pytesseract and PIL can only successfully recognize image verification codes without noise 
path = '999.png' 
captcha = Image.open(path) 
result = pytesseract.image_to_string(captcha,lang="chi_sim") 
print(result)

Execution result: recognition succeeded

Obtain the URL address of the verification code image

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/130201117