Python 之12306网站验证码校验案例

import requests
from PIL import Image
import jsons

requests.packages.urllib3.disable_warnings()

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
session = requests.session()


# 获取验证码位置
def get_captcha_position(img_name="12303_captcha.png"):
    url = "https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand";
    try:
        response = session.get(url=url, headers=headers, verify=False)
    except ConnectionError as e:
        print(e)
    else:
        with open(img_name, "wb") as f:
            f.write(response.content)
        try:
            # 人眼查看验证码
            with Image.open(img_name) as img:
                img.show()
        except FileNotFoundError as e:
            print(e)
        else:
            # =======================================================================
            # 根据打开的图片识别验证码输入图片索引序号,有可以是1张、2张、3张图片的序号,序号之间用逗号隔开
            # 例如2,4,6,表示第1排第2,第2排第4,6张
            # ---------------------------------------
            #         |         |         |
            #    0    |    1    |    2    |     3
            #         |         |         |
            # ---------------------------------------
            #         |         |         |
            #    4    |    5    |    6    |     7
            #         |         |         |
            # ---------------------------------------
            # =======================================================================
            input_index = input('请输入验证码位置,以","分割(例如2,4,6):')
            return input_index


# 校验验证码
def check_captcha(index):
    index_list = str(index).split(",")
    # 由于12306官方验证码是验证正确验证码的坐标范围,我们取每个验证码中点的坐标(大约值)
    img_center_position = ['35,35', '105,35', '175,35', '245,35', '35,105', '105,105', '175,105', '245,105']
    right_position = []
    for i in index_list:
        right_position.append(img_center_position[int(i)])
    right_position_str = ",".join(right_position)
    check_captcha_url = "https://kyfw.12306.cn/passport/captcha/captcha-check"
    data = {
        'login_site': 'E',  # 固定的
        'rand': 'sjrand',  # 固定的
        'answer': right_position_str  # 验证码对应的中心坐标字符串序号
    }
    try:
        response = session.post(url=check_captcha_url, data=data, headers=headers, verify=False)
    except ConnectionError as e:
        print(e)
    else:
        json_result = jsons.loads(response.content)
        print(json_result)
        check_code = int(json_result['result_code'])
        # 取出验证结果,4:成功  5:验证失败  7:过期
        if check_code == 4:
            print(json_result['result_message'])
            return True
        elif check_code == 5:
            print(json_result['result_message'])
        else:
            print(json_result['result_message'])
        return False


def main():
    img_index = get_captcha_position()
    check_captcha(img_index)


if __name__ == '__main__':
    main()

结果如图:

原文地址:https://www.cnblogs.com/yang-2018/p/10960170.html