下面一段代码给大家带来了python实现12306登录功能,具体代码如下所示:
#!/usr/bin/env python import requests import urllib.parse import random import time req = requests.session() import sys import re import urllib3 import getpass # 密文输入 urllib3.disable_warnings() # 登陆------------------------- apptklist = [] newStrList = [] def signin(): ran = random.uniform(1, 0) imgUrl = ('https://kyfw.12306.cn/passport/captcha/captcha-image"locate.html", 'wb+') fd.write(response.content) yzData = { 'appid':'otn' } response = req.post('https://kyfw.12306.cn/passport/web/auth/uamtk', data=yzData, headers=headers, verify=False) print('---------------------第一次验证---------------------') print(response.text) # print('typeof response',type(response)) loginMessage = response.json()['newapptk'] print('loginMessage=', loginMessage) # 第二次验证开始++++++++++++++++++++++++++++++ yz2Data = { 'tk': loginMessage } response = req.post('https://kyfw.12306.cn/otn/uamauthclient', data=yz2Data, headers=headers,verify=False) print('---------------------第二次验证---------------------') print(response.text) apptk = response.json()['apptk'] apptklist.append(apptk) def buy(): print(newStrList) req.headers['Referer'] = 'https://kyfw.12306.cn/otn/leftTicket/init' result = req.post('https://kyfw.12306.cn/otn/login/checkUser') print('----------------购票系统--------------') print(result.text) print('验证登录状态成功checkUser') headers = { 'Referer':'https://kyfw.12306.cn/otn/leftTicket/init', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36' ' (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36', } data = { # 'secretStr':reserve[1], 'train_date':'2018-01-17', 'back_train_date':'2018-01-17', 'tour_flag':'dc', # dc 单程 'purpose_codes':'ADULT', # adult 成人票 'query_from_station_name':'成都', 'query_to_station_name':'长沙', 'undefined':'' } def ticket(): # 先登陆、然后查询车票信息 signin() url = ('https://kyfw.12306.cn/otn/leftTicket/queryZ"__main__": # ticket() # with open('./aaa.xlsx', encoding='utf-8') as f: # # print(f.read()) dic = {} context = [('IDS_ABOUT_OFFICAL_PHONE', 'Slu"color: #ff0000">总结以上所述是小编给大家介绍的Python 实现12306登录实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!