调用第三方接口识别12306验证码并自动登陆

举报
悦来客栈的老板 发表于 2020/12/29 00:58:56 2020/12/29
【摘要】 import timeimport jsonimport base64import randomimport requestsfrom bs4 import BeautifulSoup def get_pic_point(image): point_map = { '1': '37,46', '2': '110,46', '3': '181,46', '4': '253,4...

  
  1. import time
  2. import json
  3. import base64
  4. import random
  5. import requests
  6. from bs4 import BeautifulSoup
  7. def get_pic_point(image):
  8. point_map = {
  9. '1': '37,46',
  10. '2': '110,46',
  11. '3': '181,46',
  12. '4': '253,46',
  13. '5': '37,116',
  14. '6': '110,116',
  15. '7': '181,116',
  16. '8': '253,116',
  17. }
  18. url= "http://littlebigluo.qicp.net:47720/"
  19. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',}
  20. files={'pic_xxfile':('image.png',image,'image/png'),}
  21. res=requests.post(url,headers = headers,files=files)
  22. if res.status_code == 200:
  23. soup = BeautifulSoup(res.text,'lxml')
  24. points = soup.select_one('font').text
  25. return ','.join([point_map[point] for point in points.split()])
  26. else:
  27. return None
  28. def get_point_360(imgbase64):
  29. url = "http://60.205.200.159/api"
  30. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',}
  31. data = {"base64":imgbase64,}
  32. res = requests.post(url,headers = headers,json = data).json()
  33. check = res['check']
  34. data = {
  35. '=':'',
  36. 'check':check,
  37. 'img_buf':imgbase64,
  38. 'logon':1,
  39. 'type':'D',}
  40. url = "http://check.huochepiao.360.cn/img_vcode"
  41. res = requests.post(url,json = data,headers = headers).json()
  42. res = res['res']
  43. res = res.replace('(','')
  44. res = res.replace(')','')
  45. return res
  46. def check_captcha():
  47. captcha_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image64'
  48. headers = {
  49. 'Host': 'kyfw.12306.cn',
  50. 'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init',
  51. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
  52. }
  53. random_data = random.random()
  54. session = requests.Session()
  55. params = {
  56. 'login_site':'E',
  57. 'module':'login',
  58. 'rand':'sjrand',
  59. '_':random_data,
  60. }
  61. response = session.get(captcha_url,params = params,headers = headers)
  62. try:
  63. data = response.json()
  64. img_base64 = data['image']
  65. except:
  66. print ("验证码获取失败!")
  67. return
  68. #points = get_point_360(img_base64)
  69. points = get_pic_point(base64.b64decode(img_base64))
  70. params.pop('module')
  71. params['_'] = int(1000 * time.time())
  72. params['answer'] = points
  73. check_captcha = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
  74. response = session.get(check_captcha,params = params,headers = headers)
  75. data = response.json()
  76. if data['result_code'] != '4':
  77. print ("验证码识别失败!")
  78. return
  79. form_data = {
  80. 'username': "*************", #12306账号
  81. 'password': "*************", #12306密码
  82. 'appid': 'otn'
  83. }
  84. login_url = 'https://kyfw.12306.cn/passport/web/login'
  85. response = session.post(login_url, data = form_data)
  86. res = response.json()
  87. if res["result_code"] == 0:
  88. uamtk_url = 'https://kyfw.12306.cn/passport/web/auth/uamtk'
  89. response = session.post(uamtk_url, data={'appid': 'otn'})
  90. res = response.json()
  91. if res["result_code"] == 0:
  92. check_token_url = 'https://kyfw.12306.cn/otn/uamauthclient'
  93. response = session.post(check_token_url, data={'tk': res['newapptk']})
  94. data = response.json()
  95. print ("{0}!欢迎你:{1}!".format(data['result_message'],data['username']))
  96. if __name__ == '__main__':
  97. check_captcha()

 

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/87192074

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。