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')