python接口自動化之獲取cookie的方法


# conding:utf-8
import requests
from lxml import etree  #爬取網頁的功能模塊
import urllib3
urllib3.disable_warnings()


s = requests.session()

def get_it_execution():
    result = {}
    loginurl = "https://account.chsi.com.cn/passport/login"
    h1 = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
    }
    r = s.get(loginurl, headers=h1,verify=False)
    dom = etree.HTML(r.content)
    try:
        result["lt"] = dom.xpath('//input[@name="lt"]')[0].get("value")  #這里又從0開始了
        result["execution"] = dom.xpath('//input[@name="execution"]')[0].get("value")
    except:
        print("lt、execution參數獲取失敗!")
    return result

def login(result, user='13812348888', psw='123456'):
    loginurl = "https://account.chsi.com.cn/passport/login"
    h2 = {
        "Referer": loginurl,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Origin": "https://account.chsi.com.cn",
        "Content-Length": "119",
        "Cache-Control": "max-age=0",
        "Upgrade-Insecure-Requests": "1",
        "Content-Type": "application/x-www-form-urlencoded"
        }
    body = {
        "username": user,
        "password": psw,
        "rememberMe": "true",
        "lt": result["lt"],
        "execution": result["execution"],
        "_eventId": "submit"
    }
    s.post(loginurl, headers=h2,data=body, verify=False)
    cookie_dirt = requests.utils.dict_from_cookiejar(s.cookies)
    print(cookie_dirt)

if __name__ == "__main__":
    result = get_it_execution()
    login(result, user='13241474925', psw='Caonima123')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM