2018-08-29 11:19:13 +00:00
|
|
|
|
# coding=utf-8
|
2018-09-04 11:02:28 +00:00
|
|
|
|
import copy
|
|
|
|
|
import random
|
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())
|
2019-01-23 04:28:41 +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)
|
2019-01-17 07:33:10 +00:00
|
|
|
|
except Exception:
|
2019-01-16 13:20:56 +00:00
|
|
|
|
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:
|
2019-01-23 04:28:41 +00:00
|
|
|
|
print(u"验证码下载失败,可能ip被封,确认请手动请求: {0}".format(codeImgUrl))
|
2019-04-03 15:29:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getPassCodeNewOrderAndLogin1(session, imgType):
|
|
|
|
|
"""
|
|
|
|
|
获取验证码2
|
|
|
|
|
:param session:
|
|
|
|
|
:param imgType:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
if imgType == "login":
|
|
|
|
|
codeImgUrl = copy.deepcopy(session.urls["getCodeImg1"])
|
|
|
|
|
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'
|
|
|
|
|
codeImgUrlRsp = session.httpClint.send(codeImgUrl)
|
|
|
|
|
result = eval(codeImgUrlRsp.split("(")[1].split(")")[0]).get("image")
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
pass
|