日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何處理掉12306的驗證碼

本文實例講述了python實現(xiàn)破解12306圖片驗證碼的方法。分享給大家供大家參考,具體如下:

不知從何時起,12306的登錄驗證碼竟然變成了按字找圖,可以說是又提高了一個等次,竟然把圖像識別都用上了。不過有些圖片,不得不說有些變態(tài),圖片的清晰圖就更別說了,明顯是從網(wǎng)絡(luò)上的圖庫中搬過來的。

相關(guān)推薦:《Python基礎(chǔ)教程》

誰知沒多久,網(wǎng)絡(luò)就驚現(xiàn)破解12306圖片驗證碼的Python代碼了,作為一個愛玩愛刺激的網(wǎng)蟲,當然要分享一份過來。

代碼大致流程:

1、將驗證碼圖片下載下來,然后切圖;

2、利用百度識圖進行圖片分析;

3、再利用正則表達式來取出百度識圖的關(guān)鍵字,最后輸出。

代碼:

#!/usr/bin/python
# # FileName  : fuck12306.py
# # Author   : MaoMao Wang 
# # Created   : Mon Mar 16 22:08:41 2015 by ShuYu Wang
# # Copyright  : Feather (c) 2015
# # Description : fuck fuck 12306
# # Time-stamp: <2015-03-17 10:57:44 andelf>
from PIL import Image
from PIL import ImageFilter
import urllib
import urllib2
import re
import json
# hack CERTIFICATE_VERIFY_FAILED
# https://github.com/mtschirs/quizduellapi/issues/2
import ssl
if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context
UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome
/41.0.2272.89 Safari/537.36"
pic_url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.21191171556711197"
def get_img():
    resp = urllib.urlopen(pic_url)
    raw = resp.read()
    with open("./tmp.jpg", 'wb') as fp:
        fp.write(raw)
    return Image.open("./tmp.jpg")
def get_sub_img(im, x, y):
    assert 0 <= x <= 3
    assert 0 <= y <= 2
    WITH = HEIGHT = 68
    left = 5 + (67 + 5) * x
    top = 41 + (67 + 5) * y
    right = left + 67
    bottom = top + 67
    return im.crop((left, top, right, bottom))
def baidu_stu_lookup(im):
    url = "http://stu.baidu.com/n/image?fr=html5&needRawImageUrl=true&id=WU_FILE_0&name=233.png&type=
    image%2Fpng&lastModifiedDate=Mon+Mar+16+2015+20%3A49%3A11+GMT%2B0800+(CST)&size="
    im.save("./query_temp_img.png")
    raw = open("./query_temp_img.png", 'rb').read()
    url = url + str(len(raw))
    req = urllib2.Request(url, raw, {'Content-Type':'image/png', 'User-Agent':UA})
    resp = urllib2.urlopen(req)
    resp_url = resp.read()   # return a pure url
    url = "http://stu.baidu.com/n/searchpc?queryImageUrl=" + urllib.quote(resp_url)
    req = urllib2.Request(url, headers={'User-Agent':UA})
    resp = urllib2.urlopen(req)
    html = resp.read()
    return baidu_stu_html_extract(html)
def baidu_stu_html_extract(html):
    #pattern = re.compile(r'', re.DOTALL | re.MULTILINE)
    pattern = re.compile(r"keywords:'(.*?)'")
    matches = pattern.findall(html)
    if not matches:
        return '[UNKNOWN]'
    json_str = matches[0]
    json_str = json_str.replace('\\x22', '"').replace('\\\\', '\\')
    #print json_str
    result = [item['keyword'] for item in json.loads(json_str)]
    return '|'.join(result) if result else '[UNKNOWN]'
def ocr_question_extract(im):
    # git@github.com:madmaze/pytesseract.git
    global pytesseract
    try:
        import pytesseract
    except:
        print "[ERROR] pytesseract not installed"
        return
    im = im.crop((127, 3, 260, 22))
    im = pre_ocr_processing(im)
    # im.show()
    return pytesseract.image_to_string(im,).strip()
def pre_ocr_processing(im):
    im = im.convert("RGB")
    width, height = im.size
    white = im.filter(ImageFilter.BLUR).filter(ImageFilter.MaxFilter(23))
    grey = im.convert('L')
    impix = im.load()
    whitepix = white.load()
    greypix = grey.load()
    for y in range(height):
        for x in range(width):
            greypix[x,y] = min(255, max(255 + impix[x,y][0] - whitepix[x,y][0],
                255 + impix[x,y][1] - whitepix[x,y][1],
                255 + impix[x,y][2] - whitepix[x,y][2]))
    new_im = grey.copy()
    binarize(new_im, 150)
    return new_im
def binarize(im, thresh=120):
    assert 0 < thresh < 255
    assert im.mode == 'L'
    w, h = im.size
    for y in xrange(0, h):
        for x in xrange(0, w):
            if im.getpixel((x,y)) < thresh:
                im.putpixel((x,y), 0)
            else:
                im.putpixel((x,y), 255)
if __name__ == '__main__':
    im = get_img()
    #im = Image.open("./tmp.jpg")
    print 'OCR Question:', ocr_question_extract(im)
    for y in range(2):
        for x in range(4):
            im2 = get_sub_img(im, x, y)
            result = baidu_stu_lookup(im2)
            print (y,x), result

網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:python如何處理掉12306的驗證碼
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/dppiesp.html