Python模擬登錄12306的方法
本文實(shí)例講述了Python模擬登錄12306的方法。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re;
import sys;
import cookielib;
import urllib;
import urllib2;
import optparse;
import json;
import httplib2;
reload(sys)
sys.setdefaultencoding('utf8');
def Login():
cj = cookielib.CookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
urllib2.install_opener(opener);
print "--------------[step1] to get cookie";
Url = "https://kyfw.12306.cn/otn/login/init";
resp = urllib2.urlopen(Url);
for index, cookie in enumerate(cj):
print '[',index, ']',cookie;
print "--------------[step2] to get code";
Url2 = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand";
resp2 = urllib2.urlopen(Url2);
#respInfo2 = resp2.info();
#print "respInfo=",respInfo2;
with open("code.png", "wb") as image:
image.write(resp2.read())
codeStr = sys.stdin.readline();
codeStr = codeStr[:-1]
print "--------------[step3] to check code";
ajax_url = "https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn";
dc = {
'randCode' : codeStr,
'rand' : "sjrand"
};
request = urllib2.Request(ajax_url, urllib.urlencode(dc))
request.add_header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
request.add_header('X-Requested-With','XMLHttpRequest')
request.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36')
request.add_header('Referer','https://kyfw.12306.cn/otn/login/init')
request.add_header('Accept','*/*')
request.add_header('Accept-Encoding','gzip, deflate')
f = urllib2.urlopen(request)
print(f.read())
print "--------------[step4] to login";
LoginUrl = "http://kyfw.12306.cn/otn/login/loginAysnSuggest";
dc = {
'randCode' : codeStr,
'userDTO.password' : "sunyuke1989",
'loginUserDTO.user_name': "sunyuke@qq.com"
};
req = urllib2.Request(LoginUrl, urllib.urlencode(dc));
req.add_header('Content-Type', "application/x-www-form-urlencoded");
req.add_header('X-Requested-With','XMLHttpRequest');
req.add_header('Origin','https://kyfw.12306.cn');
req.add_header('Referer','https://kyfw.12306.cn/otn/login/init');
req.add_header('Accept','*/*');
req.add_header('Accept-Encoding','gzip, deflate');
req.add_header('Connection','keep-live');
request.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36')
resp = urllib2.urlopen(req);
print(resp.read().encode('gb18030'));
LoginingUrl = "https://kyfw.12306.cn/otn/login/userLogin";
req = urllib2.Request(LoginingUrl, "");
print "--------------[step5] to QueryUserInfo";
LoginingUrl = "https://kyfw.12306.cn/otn/modifyUser/initQueryUserInfo";
req = urllib2.Request(LoginingUrl, "");
resp = urllib2.urlopen(req);
info = resp.read();
print(resp.read().encode('gb18030'));
if __name__=="__main__":
Login();
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
- 利用python代碼寫的12306訂票代碼
- Python腳本實(shí)現(xiàn)12306火車票查詢系統(tǒng)
- 使用Python神器對付12306變態(tài)驗(yàn)證碼
- 使用Python+Splinter自動(dòng)刷新?lián)?2306火車票
- Python實(shí)現(xiàn)破解12306圖片驗(yàn)證碼的方法分析
- python實(shí)現(xiàn)12306火車票查詢器
- python編程實(shí)現(xiàn)12306的一個(gè)小爬蟲實(shí)例
- Python使用面向?qū)ο蠓绞絼?chuàng)建線程實(shí)現(xiàn)12306售票系統(tǒng)
- python+pyqt實(shí)現(xiàn)12306圖片驗(yàn)證效果
- python自動(dòng)查詢12306余票并發(fā)送郵箱提醒腳本
相關(guān)文章
Python利用多線程同步鎖實(shí)現(xiàn)多窗口訂票系統(tǒng)(推薦)
這篇文章主要介紹了Python利用多線程同步鎖實(shí)現(xiàn)多窗口訂票系統(tǒng),主要是利用threading.lock()通過實(shí)例代碼相結(jié)合給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Python基礎(chǔ)之循環(huán)語句相關(guān)知識(shí)總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python基礎(chǔ)的相關(guān)知識(shí),文章圍繞著Python循環(huán)語句展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06pytorch模型預(yù)測結(jié)果與ndarray互轉(zhuǎn)方式
今天小編就為大家分享一篇pytorch模型預(yù)測結(jié)果與ndarray互轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01PyCharm2020.1.1與Python3.7.7的安裝教程圖文詳解
這篇文章主要介紹了PyCharm2020.1.1與Python3.7.7的安裝教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Django3基于WebSocket實(shí)現(xiàn)WebShell的詳細(xì)過程
最近工作中需要開發(fā)前端操作遠(yuǎn)程虛擬機(jī)的功能,簡稱WebShell,普通應(yīng)用大部分用的都是wsgi.py配合nginx部署線上服務(wù). 這次主要使用asgi.py,具體實(shí)現(xiàn)過程跟隨小編一起看看吧2021-08-08