1. 程式人生 > >requests模擬登入百度

requests模擬登入百度


# coding:utf-8
"""
rsa加密演算法一竅不通,沒有對密碼加密而是直接使用的Firefox中已加密的密碼
因為用的是登陸時候的加密密碼,所以rsakey等都要是登入時的值
"""
import requests
import re,os
from PIL import Image
import cookielib
import rsa,base64


agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
headers = {
    "Host":"passport.baidu.com",
    "User-Agent":agent,
    "Referer":"https://www.baidu.com/"
}
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='Baidu_cookies')

try:
    session.cookies.load(ignore_discard=True)
except:
    print "cookie未載入!"

def get_token():
    url = "https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true"
    session.get(url, headers=headers)
    html = session.get(url,headers=headers).content
    patt = re.compile(r"login_token='(.*?)';")
    token =re.findall(patt,html)[0]
    return token
def get_codestring(name,password):
    url = "https://passport.baidu.com/v2/api/?login"
    post_data1 = {

        "staticpage": "https://www.baidu.com/cache/user/html/v3Jump.html",
        "charset": "UTF-8",
        "token": get_token(),
        "tpl": "mn",
        "subpro": "",
        "apiver": "v3",
        "tt": "1477293628520",#登入時的tt
        "codestring": "",
        "safeflg": "0",
        "u": "https://www.baidu.com/",
        "isPhone": "false",
        "detect": "1",
        "gid": "F869E08-5340-42BC-B189-058C960376E9",#登入時的gid
        "quick_user": "0",
        "logintype": "dialogLogin",
        "logLoginType": "pc_loginDialog",
        "idc": "",
        "loginmerge": "true",
        "splogin": "rate",
        "username": name,
        "password": password,
        "mem_pass": "on",
        "rsakey": "VlDcAx3iLF0Ktr6AJe1Db76JFJ0kf7RT",#登入時的rsakey
        "crypttype": "12",
        "ppui_logintime": "6055",#登入時的值
        "countrycode": "",
        "callback": "parent.bd__pcbs__je2d8f",#登入時的值
    }
    login_html = session.post(url,data=post_data1,headers=headers)
    verifycode = re.findall(r'codeString=(.*?)&userName',login_html.text)[0]
    #print verifycode
    return verifycode


def get_verifycode(name,password,codestring):
    img_url = "https://passport.baidu.com/cgi-bin/genimage?" + codestring
    r = session.get(img_url,headers=headers).content

    with open('Baidu_captcha.jpg','wb') as f:
        f.write(r)
        f.close()
    try:
        image = Image.open('Baidu_captcha.jpg')
        image.show()
        image.close()
    except:
        print "請到目錄%s檢視Baidu_captcha.jpg並輸入"%os.path.abspath('Baidu_captcha.jpg')
    verifycode = raw_input("請輸入驗證碼:\n> ")
    return verifycode

def islogin():
    url = "http://i.baidu.com/"
    html = session.get(url,headers=headers).text
    #print html
    pattern = re.compile(r'<a class="header-tu-img header-tool-user-nick" href="javascript:;" />(.*?)</a>',re.S)
    item = re.findall(pattern,html)
    for content in item:
        return content.strip()



def login_Baidu(name,password,codestring):
    post_url = "https://passport.baidu.com/v2/api/?login"
    post_data = {
        "staticpage": "https://www.baidu.com/cache/user/html/v3Jump.html",
        "charset": "UTF-8",
        "token": get_token(),
        "tpl": "mn",
        "subpro": "",
        "apiver": "v3",
        "tt": "1476934121419",#登入時的值
        "codestring": codestring,  # 驗證碼編號,動態
        "safeflg": "0",
        "u": "https://www.baidu.com/",
        "isPhone": "false",
        "detect": "1",
        "gid": "6ABF1D6-2FFE-4D4F-BF10-F2A2153F26F9",#登入時的值
        "quick_user": "0",
        "logintype": "dialogLogin",
        "logLoginType": "pc_loginDialog",
        "idc": "",
        "loginmerge": "true",
        "splogin": "rate",
        "username": name,  # 加密了
        "password": password,  # 加密了
        "verifycode": get_verifycode(name,password,codestring),  # 驗證碼
        "rsakey": "6rzx1Dkc3e6sNtDbcOlv4DxYQg4O1DG7",#登入時的值
        "crypttype": "12",
        "ppui_logintime": "74490",#登入時的值
        "countrycode": "",
        "callback": "parent.bd__pcbs__brxpoi",#登入時的值

    }
    login_page = session.post(post_url,data=post_data,headers=headers)
    print login_page.status_code
    if islogin():
        print "模擬登入成功!"
    session.cookies.save(ignore_discard=True, ignore_expires=True)

if __name__ == '__main__':
    if islogin():
        print "cookie登入成功!"
    else:
        name = raw_input("請輸入賬號:>")
        password = raw_input("請輸入密碼:>")
        codestring = get_codestring(name,password)
        login_Baidu(name,password,codestring)#統一驗證碼編號,防止獲取驗證碼的編號與post_data中的不一致
        print islogin()