1. 程式人生 > >12306自動登錄

12306自動登錄

校驗 存儲 ssp 成功 沒有 pen bin 驗證碼 對象

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
"""
12306自動登錄: 
    - 先校驗驗證碼
    - 再校驗用戶名密碼
"""

import ssl
import json
import urllib
import urllib2
import cookielib

# 用來保存cookie
c = cookielib.LWPCookieJar() # 存儲cookie的對象
cookie = urllib2.HTTPCookieProcessor(c)
opener = urllib2.build_opener(cookie)
urllib2.install_opener(opener)


# 跳過證書驗證
ssl._create_default_https_context = ssl._create_unverified_context

headers = {
    ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36‘
}

# 下載驗證碼圖片
def getCode():
    req = urllib2.Request(‘https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.5074854291360469‘)
    req.headers = headers
    # codeFile = urllib2.urlopen(req).read()
    codeFile = opener.open(req).read()
    with open(‘code.png‘,‘wb‘) as fw:
        fw.write(codeFile)

def login():
    """
    - 校驗驗證碼
        - 沒攜帶cookie:          {"result_message":"驗證碼校驗失敗,信息為空","result_code":"8"}
        - 攜帶cookie但點錯了:     {"result_message":"驗證碼校驗失敗","result_code":"5"}
        - 攜帶cookie並且點擊正確:  {"result_message":"驗證碼校驗成功","result_code":"4"}
        - 停留時間過長:           {"result_message":"驗證碼已經過期","result_code":"7"}
    
    - 校驗用戶名密碼:
        - 密碼輸入錯誤:           {"result_message":"密碼輸入錯誤。如果輸錯次數超過4次,用戶將被鎖定。","result_code":1}
        - 用戶不存在:             {"result_message":"登錄名不存在。","result_code":1}
        - 密碼輸入正確:           {"result_message":"登錄成功","result_code":0,"uamtk":"tWDQtPie_z22IWMknmFOymUpDRzvLE4CfzREJBzS9NwrwL2L0"} 
    
    :return: 
    """
    getCode()
    # 坐標型驗證碼:選幾個就有幾對(x,y)值;坐標原點在左上角
    code = raw_input(‘請輸入驗證碼>>> ‘)
    # POST請求
    form_data = {
        ‘answer‘: code,
        ‘login_site‘: ‘E‘,
        ‘rand‘: ‘sjrand‘,
    }
    code_url = ‘https://kyfw.12306.cn/passport/captcha/captcha-check‘
    form_str = urllib.urlencode(form_data)  # 把字典轉換成查詢字符串,這個方法在urllib2 裏是沒有的
    req = urllib2.Request(code_url)
    # rsp.add_header(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36‘)
    req.headers = headers
    # html = urllib2.urlopen(req,data=form_str).read() # url 或者 對象
    html = opener.open(req,data=form_str).read() # url 或者 對象
    print html  # {"result_message":"驗證碼校驗失敗,信息為空","result_code":"8"}
    code_ret =  json.loads(html)
    if ‘4‘ == code_ret[‘result_code‘]:
        print "驗證碼校驗成功,進行校驗用戶名密碼..."
        req_login = urllib2.Request(‘https://kyfw.12306.cn/passport/web/login‘)
        req_login.headers = headers
        login_data_dict = {
            ‘username‘:‘xxxxxx‘,
            ‘password‘:‘123456789‘,
            ‘appid‘:‘otn‘,
        }
        login_data_str = urllib.urlencode(login_data_dict)
        login_html = urllib2.urlopen(req_login,data=login_data_str).read()
        print login_html
    else:
        print "驗證碼校驗失敗!!!"

if __name__ == ‘__main__‘:
    login()

  

12306自動登錄