• 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')
  • 相关阅读:
    01《UML大战需求分析》阅读笔记之一
    软件构架实践阅读笔记四(读后感)
    软件构架实践阅读笔记三(读后感)
    软件构架实践阅读笔记二(读后感)
    软件构架实践阅读笔记一(读后感)
    阅读笔记 火球UML大战需求分析4
    阅读笔记 火球UML大战需求分析3
    阅读笔记 火球——UML大战需求分析 2
    课堂讨论
    学习进度条
  • 原文地址:https://www.cnblogs.com/huaniaoyuchong/p/13964565.html
Copyright © 2020-2023  润新知