• 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')
  • 相关阅读:
    WinForm 无边框窗体 拖动工作区移动窗体
    [CSS]火狐和IE对css样式解释的差异
    md类型文件迁移至Notion(img资源也可以上传)
    google推出notebook软件
    刚收到几个google analytics 邀请,有真正需要的我可以送给他一个
    最近用drupal做了一个CMS网站
    google adsense 又增加了Picasa的推介,我已加上
    googlepages空间的一个bug
    近段时间比较郁闷
    开复与学生面对面
  • 原文地址:https://www.cnblogs.com/huaniaoyuchong/p/13964565.html
Copyright © 2020-2023  润新知