亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python模擬登錄12306的方法

 更新時(shí)間:2014年12月30日 14:56:40   投稿:shichen2014  
這篇文章主要介紹了Python模擬登錄12306的方法,是非常實(shí)用的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Python模擬登錄12306的方法。分享給大家供大家參考。

具體實(shí)現(xiàn)方法如下:

復(fù)制代碼 代碼如下:

#!/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ì)有所幫助。

相關(guān)文章

  • Python中find函數(shù)的詳細(xì)使用方法

    Python中find函數(shù)的詳細(xì)使用方法

    在Python中find()函數(shù)用于檢測字符串中是否包含子字符串sub,如果指定start(開始)和end(結(jié)束)范圍,則檢查是否包含在指定范圍內(nèi),這篇文章主要給大家介紹了關(guān)于Python中find函數(shù)的詳細(xì)使用方法,需要的朋友可以參考下
    2023-05-05
  • pycharm 使用心得(一)安裝和首次使用

    pycharm 使用心得(一)安裝和首次使用

    PyCharm 是我用過的python編輯器中,比較順手的一個(gè)。而且可以跨平臺(tái),在macos和windows下面都可以用,這點(diǎn)比較好。
    2014-06-06
  • Python利用多線程同步鎖實(shí)現(xiàn)多窗口訂票系統(tǒng)(推薦)

    Python利用多線程同步鎖實(shí)現(xiàn)多窗口訂票系統(tǒng)(推薦)

    這篇文章主要介紹了Python利用多線程同步鎖實(shí)現(xiàn)多窗口訂票系統(tǒng),主要是利用threading.lock()通過實(shí)例代碼相結(jié)合給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python基礎(chǔ)之循環(huán)語句相關(guān)知識(shí)總結(jié)

    Python基礎(chǔ)之循環(huán)語句相關(guān)知識(shí)總結(jié)

    今天給大家?guī)淼氖顷P(guān)于Python基礎(chǔ)的相關(guān)知識(shí),文章圍繞著Python循環(huán)語句展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Python支持異步的列表解析式

    Python支持異步的列表解析式

    這篇文章主要為大家介紹了Python支持異步的列表解析式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Python 操作 ElasticSearch的完整代碼

    Python 操作 ElasticSearch的完整代碼

    python提供了操作ElasticSearch 接口,因此要用python來操作ElasticSearch,這篇文章主要介紹了Python 操作 ElasticSearch,需要的朋友可以參考下
    2019-08-08
  • Python中的Matplotlib模塊入門教程

    Python中的Matplotlib模塊入門教程

    這篇文章主要介紹了Python中的Matplotlib模塊入門教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • pytorch模型預(yù)測結(jié)果與ndarray互轉(zhuǎn)方式

    pytorch模型預(yù)測結(jié)果與ndarray互轉(zhuǎn)方式

    今天小編就為大家分享一篇pytorch模型預(yù)測結(jié)果與ndarray互轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • PyCharm2020.1.1與Python3.7.7的安裝教程圖文詳解

    PyCharm2020.1.1與Python3.7.7的安裝教程圖文詳解

    這篇文章主要介紹了PyCharm2020.1.1與Python3.7.7的安裝教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Django3基于WebSocket實(shí)現(xiàn)WebShell的詳細(xì)過程

    Django3基于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

最新評(píng)論