12306/inter/GetPassCodeNewOrderAndLogin.py

38 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# coding=utf-8
import copy
import random
import time
def getPassCodeNewOrderAndLogin(session, imgType):
"""
下载验证码
:param session:
:param imgType: 下载验证码类型login=登录验证码,其余为订单验证码
:return:
"""
if imgType == "login":
codeImgUrl = copy.deepcopy(session.urls["getCodeImg"])
codeImgUrl["req_url"] = codeImgUrl["req_url"].format(random.random())
else:
codeImgUrl = copy.deepcopy(session.urls["codeImgByOrder"])
codeImgUrl["req_url"] = codeImgUrl["req_url"].format(random.random())
print(u"下载验证码...")
img_path = './tkcode.png'
result = session.httpClint.send(codeImgUrl)
try:
if isinstance(result, dict):
print(u"下载验证码失败, 请手动检查是否ip被封或者重试请求地址https://kyfw.12306.cn{}".format(codeImgUrl.get("req_url")))
return False
else:
print(u"下载验证码成功")
try:
with open(img_path, 'wb', encoding="utf-8") as img:
img.write(result)
except Exception:
with open(img_path, 'wb') as img:
img.write(result)
return result
except OSError:
print(u"验证码下载失败可能ip被封确认请手动请求: {0}".format(codeImgUrl))