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();
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- 利用python代碼寫(xiě)的12306訂票代碼
- Python腳本實(shí)現(xiàn)12306火車(chē)票查詢(xún)系統(tǒng)
- 使用Python神器對(duì)付12306變態(tài)驗(yàn)證碼
- 使用Python+Splinter自動(dòng)刷新12306火車(chē)票
- Python實(shí)現(xiàn)破解12306圖片驗(yàn)證碼的方法分析
- python實(shí)現(xiàn)12306火車(chē)票查詢(xún)器
- python編程實(shí)現(xiàn)12306的一個(gè)小爬蟲(chóng)實(shí)例
- Python使用面向?qū)ο蠓绞絼?chuàng)建線(xiàn)程實(shí)現(xiàn)12306售票系統(tǒng)
- python+pyqt實(shí)現(xiàn)12306圖片驗(yàn)證效果
- python自動(dòng)查詢(xún)12306余票并發(fā)送郵箱提醒腳本
相關(guān)文章
Python實(shí)現(xiàn)將列表拆分為大小為N的塊
這篇文章主要為大家整理了一些常見(jiàn)的Python實(shí)現(xiàn)將列表拆分為大小為N的塊的方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,有需要的小伙伴可以了解下2023-09-09
pandas factorize實(shí)現(xiàn)將字符串特征轉(zhuǎn)化為數(shù)字特征
今天小編就為大家分享一篇pandas factorize實(shí)現(xiàn)將字符串特征轉(zhuǎn)化為數(shù)字特征,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python檢測(cè)兩個(gè)文本文件相似性的三種方法
檢測(cè)兩個(gè)文本文件的相似性是一個(gè)常見(jiàn)的任務(wù),可以用于文本去重、抄襲檢測(cè)等場(chǎng)景,Python 提供了多種方法來(lái)實(shí)現(xiàn)這一功能,x下面小編就來(lái)簡(jiǎn)單介紹一下吧2025-03-03
Django權(quán)限機(jī)制實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Django權(quán)限機(jī)制實(shí)現(xiàn)代碼詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Python3.2模擬實(shí)現(xiàn)webqq登錄
這篇文章主要介紹了Python模擬實(shí)現(xiàn)webqq登錄的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
python實(shí)現(xiàn)批量壓縮指定目錄下的文件夾
這篇文章主要介紹了利用Python實(shí)現(xiàn)批量壓縮指定目錄下的文件夾的示例代碼,文中代碼示例講解詳細(xì),感興趣的小伙伴快跟隨小編一起動(dòng)手試一試2023-08-08

