Python 實(shí)現(xiàn)12306登錄功能實(shí)例代碼
下面一段代碼給大家?guī)砹藀ython實(shí)現(xiàn)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?' '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('請輸入驗證碼的坐標(biāo):') 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('驗證碼校驗失敗,請注意填寫正確的坐標(biāo)') 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('----------------購票系統(tǒng)--------------') print(result.text) print('驗證登錄狀態(tài)成功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())
總結(jié)
以上所述是小編給大家介紹的Python 實(shí)現(xiàn)12306登錄實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用 Python 的 pprint庫格式化和輸出列表和字典的方法
pprint是"pretty-print"的縮寫,使用 Python 的標(biāo)準(zhǔn)庫 pprint 模塊,以干凈的格式輸出和顯示列表和字典等對象,這篇文章主要介紹了如何使用 Python 的 pprint庫格式化和輸出列表和字典,需要的朋友可以參考下2023-05-05Python利用lxml庫實(shí)現(xiàn)XML高級處理詳解
在Python的世界中,lxml是處理XML和HTML的一款強(qiáng)大且易用的庫,本文主要介紹了如何解析、創(chuàng)建、修改XML文檔,如何使用XPath查詢,以及如何解析大型XML文檔,需要的可以參考下2023-08-08Python計算一個點(diǎn)到所有點(diǎn)的歐式距離實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python計算一個點(diǎn)到所有點(diǎn)的歐式距離實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python使用FTP上傳文件的實(shí)現(xiàn)示例
本文主要介紹了Python使用FTP上傳文件的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03opencv 圖像腐蝕和圖像膨脹的實(shí)現(xiàn)
這篇文章主要介紹了opencv 圖像腐蝕和圖像膨脹的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Django框架HttpResponse和HttpRequest對象學(xué)習(xí)
這篇文章主要介紹了Django框架HttpResponse和HttpRequest對象學(xué)習(xí),有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪2021-09-09