python識別網(wǎng)站驗證碼

環(huán)境搭建

基于linux/mac、Python

1.安裝Python圖像庫

    pip install Pillow

2.安裝Python庫Pytesseract

    pip install pytesseract

3.安裝Python庫tesseract

    pip install tesseract

4.安裝leptonica

下載地址:http://www.leptonica.com/download.html

    ./configure
    make
    make install

5.安裝tesseract-ocr

下載地址:https://github.com/tesseract-ocr/tesseract

    ./autogen.sh
    CPPFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib" ./configure
    make
    make install

安裝完成后,再下載tessdata,將其放置于tessdata目錄。

Python腳本:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:wdl 
# time:2017-03-08 pm

import os
import sys
import time
import requests
from PIL import Image
import pytesseract
import subprocess

code_url = "https://www.jiguang.cn/captcha/login/"

def identification_code(url):

    #獲取驗證碼并保存
    with open("captcha.jpg","wb") as i:
        i.write(requests.get(url,stream=True).content)

    #打開圖片
    im = Image.open("captcha.jpg")

    #轉(zhuǎn)化圖片為灰度圖
    im = im.convert('L')

    def initTable(threshold=140):
        table = []
        for i in range(256):
            if i < threshold:
                table.append(0)
            else:
                table.append(1)
        return table

    #灰度圖二值化
    bininaryImage = im.point(initTable(),'1')

    #將圖片轉(zhuǎn)化為文本
    return pytesseract.image_to_string(bininaryImage,lang="eng",config="-psm 7")

print(identification_code(code_url))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個 Awesome - XXX 系列...
    aimaile閱讀 26,839評論 6 427
  • GitHub 上有一個 Awesome - XXX 系列的資源整理,資源非常豐富,涉及面非常廣。awesome-p...
    若與閱讀 19,332評論 4 417
  • 環(huán)境管理管理Python版本和環(huán)境的工具。p–非常簡單的交互式python版本管理工具。pyenv–簡單的Pyth...
    MrHamster閱讀 3,957評論 1 61
  • 最近因為課外學(xué)習(xí)的緣故,重新認(rèn)識了一本久有耳聞的書籍——《烏合之眾》,不厚的一本書卻因為可能是因為是外文翻譯過來的...
    崔澤的喜東東閱讀 556評論 0 1
  • 早上到公司,濤哥叫我看一個問題。我開心的回答:“哎!”全程交流很愉快,今天一天也感覺很順利。當(dāng)別人叫我們的時候,如...
    玉露君閱讀 209評論 0 4

友情鏈接更多精彩內(nèi)容