Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What to do when Python crawler encounters CAPTCHA

Shulou Source: shulou.com Published: 2022-06-01 20:17:23 10月02日 Update

This article introduces the knowledge about "Python crawler encounters Captcha how to solve it." In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

I. Preface

The registration page of China Zhiwang uses this Captcha. The page is as follows:

II. Preparatory work

1 goal

Taking the Captcha of Zhiwang as an example, OCR (Optical Character Recognition) technology is used to identify the graphic Captcha.

2 Installing tesseract

2.2 Download tesseract-ocr-setup-3.05.01.exe

2.3 installation considerations

Check the Additional language data (download) option to recognize multiple languages.

3 Install tesserocr

pip install tesserocr pillow

After Tesseract-OCR is installed, copy the tesseract-OCR folder from D:\Program Files (x86)\Tesseract-OCR to the following directory

E:\WebSpider\venv\Scripts

4Get Captcha

Save the Captcha graphic locally and name it code.jpg

Three battles.

1 actual combat

1.1 code

import tesserocrfrom PIL import Image image = Image.open('code.jpg')result = tesserocr.image_to_text(image)print(result) image = Image.open('code1.jpg')result = tesserocr.image_to_text(image)print(result) image = Image.open('code2.jpg')result = tesserocr.image_to_text(image)print(result)

1.2 effect

E:\WebSpider\venv\Scripts\python.exe E:/WebSpider/8_1.pyDTKDJR42PFRT

1.3 description

code.jpg is DTKT

code1.jpg is JR42

code2.jpg is PFRT

Comparing the results with actual pictures, the accuracy rate is still relatively high.

2 Combat 2

2.1 code

import tesserocr print(tesserocr.file_to_text('code.jpg'))print(tesserocr.file_to_text('code1.jpg'))print(tesserocr.file_to_text('code2.jpg'))

2.2 effect

E:\WebSpider\venv\Scripts\python.exe E:/WebSpider/8_1.pyDTKD.ll? 42FFKT

2.3 description

code.jpg is DTKT

code1.jpg is JR42

code2.jpg is PFRT

Comparing the results with actual pictures, the accuracy rate is not very high.

3 Combat 3

3.1 code

import tesserocrfrom PIL import Image image = Image.open('code2.jpg') image = image.convert('L')threshold = 127table = []for i in range(256): if i < threshold: table.append(0) else: table.append(1) image = image.point(table, '1')image.show() result = tesserocr.image_to_text(image)print(result)

3.2 effect

E:\WebSpider\venv\Scripts\python.exe E:/WebSpider/8_1.pyPFRT"Python crawler encountered Captcha how to solve" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Verification actual combat code reality effects crawlers content graphics pictures more accuracy catalogue knowledge results pages knowledge net practical successful learning next China knowledge Network Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi Shulou Technology MariaDB macOS