Python uses Tesseract library to identify and verify (24)

(1) About Tesseract

Tesseract is an OCR Library (OCR is the abbreviation of Optical Character Recognition in English). It is used to scan the text data, and then analyze and process the image file to obtain the text and layout information. Tesseract is currently recognized as the best and relatively accurate OCR library.

(2) Use of Tesseract

1. Download and install Tesseract: Click to download

2. Set environment variables under Windows system:

#Configure environment variables according to the path of the download installation file
set TESSDATA_PREFIX F:\Tesseract-OCR\

3. Install pyteseract module

pip install pytesseract

4. How to introduce the testeract.exe application in Python script:

pytesseract.pytesseract.tesseract_cmd = r'F:\Tesseract-OCR\tesseract.exe'

5. Case demonstration

Recognize the following picture text:

import pytesseract
from PIL import Image
#1. Introduce Tesseract program
pytesseract.pytesseract.tesseract_cmd = r'F:\Tesseract-OCR\tesseract.exe'
#2. Use the Open() function under the Image module to open the picture
image = Image.open('6.jpg',mode='r')
print(image)
#3. Recognize pictures and words
code= pytesseract.image_to_string(image)
print(code)

Results demonstration:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=611x210 at 0x1A5DFDCB4A8>
Google

Note: for example, the verification code generated by the tesseact OCR engine can't recognize its content. If you need to crawl the data in the Douban, you need to enter the verification code manually:

(3) Simulated Login Zhihu source code

import requests
import time
import pytesseract
from PIL import Image
from bs4 import BeautifulSoup

def captcha(data):
    with open('captcha.jpg','wb') as fp:
        fp.write(data)
    time.sleep(1)
    image = Image.open("captcha.jpg")
    text = pytesseract.image_to_string(image)
    print "The verification code after machine identification is:" + text
    command = raw_input("Please input Y It means that you agree to use it. Press other keys to re-enter by yourself:")
    if (command == "Y" or command == "y"):
        return text
    else:
        return raw_input('Enter verification code:')

def zhihuLogin(username,password):

    # Build a session object to save the Cookie value
    sessiona = requests.Session()
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'}

    # First get the page information, find the data that needs POST (and record the Cookie of the current page)
    html = sessiona.get('https://www.zhihu.com/#signin', headers=headers).content

    # Find the input tag whose name attribute value is_xsrf, and take out the value in value
    _xsrf = BeautifulSoup(html ,'lxml').find('input', attrs={'name':'_xsrf'}).get('value')

    # Take out the verification code. The value after r is the Unix timestamp, time.time()
    captcha_url = 'https://www.zhihu.com/captcha.gif?r=%d&type=login' % (time.time() * 1000)
    response = sessiona.get(captcha_url, headers = headers)


    data = {
        "_xsrf":_xsrf,
        "email":username,
        "password":password,
        "remember_me":True,
        "captcha": captcha(response.content)
    }

    response = sessiona.post('https://www.zhihu.com/login/email', data = data, headers=headers)
    print response.text

    response = sessiona.get('https://www.zhihu.com/people/maozhaojun/activities', headers=headers)
    print response.text


if __name__ == "__main__":
    #username = raw_input("username")
    #password = raw_input("password")
    zhihuLogin('xxxx@qq.com','ALAxxxxIME')

Tags: Windows Session pip Python

Posted on Fri, 03 Apr 2020 08:58:10 -0700 by wiggst3r