python接口自动化(二十四) 参数关联和JSESSIONID(上个接口返回数据作为下个接口请求参数)

前言

参数关联是接口测试和性能测试最为重要的一个步骤,很多接口的请求参数是动态的,并且需要从上一个接口的返回值里面取出来,一般只能用一次就失败了。

最常见的案例就是网站的登录案例,很多网站的登录并不仅仅只传username和psw两个参数,往往有其它的动态参数。

有时候还需要带上cookies参数,如JSESSIONID

登录参数

首先分析下目标网站 学信网:https://account.chsi.com.cn/passport/login的登录接口请求参数

先随便输入账号和密码,使用fiddler工具抓包查看请求参数,有两个参数是网页自动给的参数(用户没输入)

lt
execution

 

关闭网页,重复上面操作,再抓包看请求参数,会发现lt,execution变了

获取接口返回数据

我们想登录的话,必须先得到lt 和 execution这2个参数,那么问题来了:这两个参数是从哪来的?

打开登录页面https://account.chsi.com.cn/passport/login直接刷新,看返回的html内容

 

接下来可以用前面学的lxml爬虫框架 https://www.cnblogs.com/canglongdao/p/13447629.html,也可以使用python正则表达式,从html中解析出lt和execution这两个值

# coding:utf-8
import requests
from lxml import etree
s=requests.session()
import re
def get_lt_execution():
    result={}
    url="https://account.chsi.com.cn/passport/login"
    headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"}
    s.headers.update(headers)
    r=s.get(url)
    #方式1 etree
    #a=etree.HTML(r.content.decode("utf-8"))
    #方式2 python正则表达式
    rs=r.text
    #print(rs)
    try:
        # result["lt"]=a.xpath("//input[@name='lt']")[0].get("value")
        # result["execution"]=a.xpath("//input[@name='execution']")[0].get("value")
        # print(result)
        a=re.findall("name="lt" value="(.+?)"",rs)
        result["lt"]=a[0]
        b=re.findall("name="execution" value="(.+?)"",rs)
        result["execution"]=b[0]
        print(result)
    except:
        print("lt,execution参数获取失败!")
    return result
if __name__=="__main__":
    result=get_lt_execution()

运行结果
注意:execution结果太长了,下面的execution有删减

{'lt': 'LT-1435350-dsfdoOqqOetcE6mYiEW7UbtvAlLTMG-cas', 'execution': '9ba87760-4f57-4857-8645-e3434790e7ab_ZXlKaGJHY2lPaUpJVXpVeE1pSjkuUVVsUE1YVkZkVnBOYkhsS01IWllTR1JqT1RnM

  

登录里面实际上会有一个非常重要的cookies参数,JSESSIONID=E0573F402E9478693F095D46D6F3667F,这个JSESSIONID也是动态的,每次重新打开页面都会变。

这个参数也是第一次访问登录页面时候,服务器会自动返回过来的。

cookies参数关联实现就非常简单了,直接用requests.session()去发个get请求就能自动保存了,所以上一步get_lt_execution()实际上也同步了cookies参数

参考代码

# coding:utf-8
import requests
from lxml import etree
s=requests.session()
def get_lt_execution():
    result={}
    url="https://account.chsi.com.cn/passport/login"
    headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"}
    s.headers.update(headers)
    r=s.get(url)
    a=etree.HTML(r.content.decode("utf-8"))
    try:
        result["lt"]=a.xpath("//input[@name='lt']")[0].get("value")
        result["execution"]=a.xpath("//input[@name='execution']")[0].get("value")
        print(result)
    except:
        print("lt,execution参数获取失败!")
    return result

def login(result,user='15807021040',pwd='123456'):
    d={
    "username":user,
    "password":pwd,
    "submit":"登录",
    "lt":result["lt"],
    "execution":result["execution"],
    "_eventId":"submit"
    }
    h={
        "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
        "Content-Length":"4166",
        "Content-Type":"application/x-www-form-urlencoded",
        "Referer":"https://account.chsi.com.cn/passport/login",
        "Cache-Control":"max-age=0",
        "Origin":"https://account.chsi.com.cn",
        "Upgrade-Insecure-Requests":"1"
    }
    url="https://account.chsi.com.cn/passport/login"
    lrs=s.post(url,data=d,headers=h)
    lrs.encoding="utf-8"
    print(lrs.text)

if __name__=="__main__":
    result=get_lt_execution()
    login(result)    

运行结果

响应结果为html内容,复制内容到文本文档,另存为.html格式,使用浏览器打开,发现登录成功。oh yeah!

 

越努力,越幸运!!! good good study,day day up!!!
原文地址:https://www.cnblogs.com/canglongdao/p/13450766.html