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()

结果如图:

最新文章

  1. Android之GridView
  2. [LintCode] Length of Last Word 求末尾单词的长度
  3. WCF基礎
  4. (转)hessian源码分析(一)------架构
  5. 百度UEditor编辑器使用教程与使用方法
  6. org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session
  7. Vijos 1040 高精度乘法
  8. markdown 数学公式
  9. Powershell的IIS管理小结
  10. phpspreadsheet导出数据到Excel
  11. 随笔 | 分布式版本控制系统Git的安装与使用
  12. Nginx + Uswgi + Django的部署
  13. Vmware安装Kali
  14. leetcode-844 Backspace String Compare
  15. EBS已安装模块
  16. P2327 [SCOI2005]扫雷
  17. Python初学者第二十天 函数(3)-递归函数及练习题
  18. MyBatis For .NET学习-问题总结
  19. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1
  20. 小Hi和小Ho的礼物

热门文章

  1. [JavaEE] Data Validation
  2. Android获取全部存储卡挂载路径
  3. Project Perfect让Swift在server端跑起来-引言(一)
  4. Cant't call setState(or forceUpdate) on an unmount component. 报错的可能性原因
  5. 从零讲Java,给你一条清晰地学习道路!该学什么就学什么!
  6. LLDB 常用命令
  7. intellij idea 写 Helloworld
  8. oracle 存储过程使用动态sql
  9. singlefile.py
  10. P3567 [POI2014]KUR-Couriers 主席树