Python 之12306网站验证码校验案例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python 之12306网站验证码校验案例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2503字,纯文字阅读大概需要4分钟。
内容图文
![Python 之12306网站验证码校验案例](/upload/InfoBanner/zyjiaocheng/814/dbd864cab77d4c198c22c42d660a4354.jpg)
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()
结果如图:
内容总结
以上是互联网集市为您收集整理的Python 之12306网站验证码校验案例全部内容,希望文章能够帮你解决Python 之12306网站验证码校验案例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。