2018-08-29 11:19:13 +00:00
|
|
|
|
# coding=utf-8
|
2018-09-04 11:02:28 +00:00
|
|
|
|
import copy
|
|
|
|
|
import random
|
|
|
|
|
import time
|
2018-08-29 11:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getPassCodeNewOrderAndLogin(session, imgType):
|
|
|
|
|
"""
|
|
|
|
|
下载验证码
|
|
|
|
|
:param session:
|
|
|
|
|
:param imgType: 下载验证码类型,login=登录验证码,其余为订单验证码
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
if imgType == "login":
|
2018-09-04 11:02:28 +00:00
|
|
|
|
codeImgUrl = copy.deepcopy(session.urls["getCodeImg"])
|
|
|
|
|
codeImgUrl["req_url"] = codeImgUrl["req_url"].format(random.random())
|
2018-08-29 11:19:13 +00:00
|
|
|
|
else:
|
2018-09-04 11:02:28 +00:00
|
|
|
|
codeImgUrl = copy.deepcopy(session.urls["codeImgByOrder"])
|
|
|
|
|
codeImgUrl["req_url"] = codeImgUrl["req_url"].format(random.random())
|
2018-08-29 11:19:13 +00:00
|
|
|
|
print (u"下载验证码...")
|
2019-01-07 09:48:41 +00:00
|
|
|
|
img_path = './tkcode.png'
|
2018-08-29 11:19:13 +00:00
|
|
|
|
result = session.httpClint.send(codeImgUrl)
|
|
|
|
|
try:
|
2018-09-04 11:02:28 +00:00
|
|
|
|
if isinstance(result, dict):
|
2019-01-08 09:04:40 +00:00
|
|
|
|
print(u"下载验证码失败, 请手动检查是否ip被封,或者重试,请求地址:https://kyfw.12306.cn{}".format(codeImgUrl.get("req_url")))
|
2018-09-04 11:02:28 +00:00
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
print(u"下载验证码成功")
|
2019-01-16 13:20:56 +00:00
|
|
|
|
try:
|
|
|
|
|
with open(img_path, 'wb', encoding="utf-8") as img:
|
|
|
|
|
img.write(result)
|
|
|
|
|
except TypeError:
|
|
|
|
|
with open(img_path, 'wb') as img:
|
|
|
|
|
img.write(result)
|
2018-12-25 03:47:20 +00:00
|
|
|
|
return result
|
2018-08-31 16:24:40 +00:00
|
|
|
|
except OSError:
|
|
|
|
|
print (u"验证码下载失败,可能ip被封,确认请手动请求: {0}".format(codeImgUrl))
|