下面一段代码给大家带来了python实现12306登录功能,具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#!/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?' 'login_site=E&module=login&rand=sjrand&%s' % ran) print (imgUrl) imgresponse = req.get(url = imgUrl, verify = False ) codeimg = imgresponse.content fn = open ( 'code.png' , 'wb' ) fn.write(codeimg) fn.close() codeStr = input ( '请输入验证码的坐标:' ) a = '' b = '' c = '' d = '' e = '' f = '' g = '' h = '' if '1' in codeStr: a = '37,37,' if '2' in codeStr: b = '100,37,' if '3' in codeStr: c = '180,37,' if '4' in codeStr: d = '250,37,' if '5' in codeStr: e = '37,100,' if '6' in codeStr: f = '100,100,' if '7' in codeStr: g = '180,100,' if '8' in codeStr: h = '250,100,' newCodeStr = a + b + c + d + e + f + g + h newStr = newCodeStr[: - 1 ] newStrList.append(newStr) url = 'https://kyfw.12306.cn/passport/captcha/captcha-check' data = { 'answer' :newStr, 'login_site' : 'E' , 'rand' : 'sjrand' } headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)' ' Chrome/63.0.3239.108 Safari/537.36' , } response = req.post(url = url, data = data, headers = headers, verify = False ) print ( '检测图片-----------' , url) print (response.text) result = response.json() if result[ 'result_code' ] = = '4' : print ( '验证码校验成功' ) else : print ( '验证码校验失败,请注意填写正确的坐标' ) signin() return userName = input ( 'Please input your userName:' ) # password = input('Please input your password:') password = getpass.getpass( 'Please input your password:' ) loginData = { 'username' :userName, 'password' :password, 'appid' : 'otn' } headers = { 'Host' : 'kyfw.12306.cn' , 'Referer' : 'https://kyfw.12306.cn/otn/login/init' , 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2)' ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 ' 'Safari/537.36' } response = req.post( 'https://kyfw.12306.cn/passport/web/login' , data = loginData, headers = headers, verify = False ) print ( '--------------登陆中--------------------' ) print ( 'https://kyfw.12306.cn/passport/web/login' ) print ( 'response' , response.text.encode( 'utf-8' ).decode( 'utf-8' )) fd = open ( "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?leftTicketDTO.train_date=2018-02-10&' 'leftTicketDTO.from_station=SZQ&leftTicketDTO.to_station=NFG&purpose_codes=ADULT' ) try : response = requests.get(url, verify = False ) result = response.json() print (result) return result[ 'data' ][ 'result' ] except Exception as e: return None if __name__ = = "__main__" : # ticket() # with open('./aaa.xlsx', encoding='utf-8') as f: # # print(f.read()) dic = {} context = [( 'IDS_ABOUT_OFFICAL_PHONE' , 'Službeni telefon' ), ( 'IDS_ABOUT_OFFICAL_WEBSITE' , 'Službeno web-mjesto' ), ( 'IDS_ABOUT_OFFICIAL_PHONE_CALL_NOT_SUPPORT' , 'Trenutni uređaj ne može pozivati.' )] for i in range ( len (context)): print (i) dic.setdefault(context[i][ 0 ],context[i][ 1 ]) print (dic) print (dic.keys()) |
总结
以上所述是小编给大家介绍的Python 实现12306登录实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://www.jianshu.com/p/f2baaf0751d9