Requests方法 -- 参数关联与JSESSION(上一个接口的返回数据作为下一个接口的请求参数)

时间:2019-09-04
本文章向大家介绍Requests方法 -- 参数关联与JSESSION(上一个接口的返回数据作为下一个接口的请求参数),主要包括Requests方法 -- 参数关联与JSESSION(上一个接口的返回数据作为下一个接口的请求参数)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

参数关联是接口测试和性能测试最为重要的一个步骤,很多接口的请求参数是动态的,并且需要从上一个接口的返回值里面取出来,一般只能用一次就失效了。
最常见的案例就是网站的登录案例,很多网站的登录并不仅仅只传username和psw两个参数,往往有其它的动态参数。
有时候还需要带上cookies参数,如JSESSIONID

登录参数

首先分析下目标网站【学信网:https://account.chsi.com.cn/passport/login】的登录接口请求参数。
先输入账号和密码,点击登录,使用fiddler工具抓包查看请求参数,用两个参数是网页自动给的参数(用户没输入)

lt LT-945609-OlQbFZ4LfFeW4gWBgJAJAbBVxwVyy7-cas
execution f8947218-de8d-4062-88a9-99e761aff8b4_ZXlKaGJHY2lPaUpJVXpVeE1pSjkuUkhwUE5uWmhaMU5hUzJOd1JVeDVlbFZUVkhSQ1Jrd3pSVzVYVWxOdGJXaEtlRVpMVjNsaFVtcFdVSGRVYm1SSFdIaHZTV2t2TlVSVFpFTlBZakJGUjNwTk9XRnlNME5rZVdWelIzQkdlWFYxTDBKVVJXWm1ORU0yYVdWUlRtUlVhVzVyYkRadVp6ZGxWa014Y2tweWNTdEdRVU5RWVdoaVQwZHJhbFJDWmpKcVVGY3hZWGhRUmxNd2FtOXhja0pKY2taM2NETkNVRkZKUjFsM04wdEljR2xMVVU1V09XRnhMMUE0WlZCeWRIRjVSVWt3TTIxdWVVMUhTazFTVFd4YWFHWlZXRlpDTW1keVZXcHpWazF4U21aRFYwVkJPVW96TTFWd1MwVkJZazFtU2pnMWNEUkRaMlkzVWpCRGRYQmhPV2xMZURjM2N6ZHNOemR5THpWSU1qQmxNR1JHTW1rM1ZVOVhVV1pMWTBGaG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WDBOc1N3cFhnZ1lzSVRYaFE=

?

?

?

退出用户后,重复上面操作,再抓包看请求参数,会发现变了

?

?

?

lt LT-946015-AtfP7ic5GSmqHffGpQbZYbv9shggDb-cas
execution 73284ef4-b155-41cf-8924-c2137733a22d_ZXlKaGJHY2lPaUpJVXpVeE1pSjkuTWl0V2NHVndaRFppVkRNd1ZqTldiRFUyTW1KSWJtUXJNbFZOYURJMGVIZHJZbFZ5U0ZORE1VOURWRzFRVTBaQlpUQXJNM1ZFUkVsS1VXdHFPUzlLT1cxSWN6Sk9ZVVpMTVRKbmEwUkNjVmM0UnpKcFdXWldjV1l5TXpoamFIQlFTalUwWlM5TmIwRm1kbkZEVTNoQmQyWlpjM0pFZWxsaU5UWmxlSEU0TUdOS2QxZFFOMmwwZW5SVlpuQmpabFF6ZDB4M1lucDZVRXhvWTB3NFpXb3dha1JHVDBsMlFXdFJiSEp5VlhwYWJEUktSRW8xZUdveEsyZDJNM0J0YlRaUVYwcFBXRU5wYVRWVFJqWnRjWEIzTVc5emRFOVJPWFJtVVdKQlFWTkNaWFl6UzBGSmRtTnFUamxSVkVoQ1Z6bFpORGMwVkhKSmVuSm5TbU5DWW1sa09XcHpXa1p3TW01dVMxbHRVbHBTUjNkU09FNVNjelpHVWtOb01DOXBVelU0VEdKQ00yWmFLMGhIY1dKV2RUWnliVFJqY1VReU1EVlFRa1kwT1c4MWNtWkxaRkpLYTFwcVRVdEhjVkJGWkhKdmEzUnBhbmRJZVRGaGIzZFliRXhTYlhKTk4wMXZWMHhuYlVoeFpISlFUekZrTkZWekwwcERVVWxvVjA1SGJqVlFhR2xyV0ZSSVdtMW1PSGRoVlhaaVNURjJPVWQxYVVkYU1IVkJVRXA0ZGs1RFpqUTBaWG8zVjAxNU5IaGtRMUJCYWxobkwwTlNhRU16WTFjdmVIYzNVbTFoVFc1QlJYWmpLM3BXYm14YWIxSm5LMWwxY0VaSGIxTnBOM3AwTDNoNWJ5OTJUM1pHV1VweE0wUkdORUZ5VDBwV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JRYkdWUE1IZDBieXRJUW1OVmJXTk9VMEU1ZDI1dlExSnFlVlJ5UVhVMWFWUk9iWEJGWjJsdlRVSnhTbmhxVjFjd2FGbFRPSEJIUkhCV2NVZEVabkpSVm1KMWFrdFRaRVZUYkRnd1psVjVURWRyTVUxUFJtbHpZblEzT1hacmJFNTBiMk5GVWsxMFJFbFNORUkxWmxGdVEyazNRemhYYlVKalJEWXliM29yUWtsR2FVbEtUMnRWWWxocFRFNHlORkJGYm5wdmFVdEVTVTFVZUV0bFYzVnJWa05xTUZZMldreHdVRU5JVkRSUlZXMTVWSFZvTDNSU2NGQXhOVm9yZGxWbFNFdGlSRXhoZDJZMlJHOXFWa1ozUW1kU0t6Qm9aR3hKYzNCTWNHRktiRE5KUm1kQ2VrNHJTRGRYV21zM2FHcE9WbGRxV0RWWFkwOUdNRlJIZDJ0WGVtOXZUMDFQT1dWSlRucFhZVXh5YldKQmNqTlZjR1J5ZUhFd1RWUlhVRTVOTkdwSWNWbzBhbFpCT1dveVdqUjVabmxTTUhOemJWaG5VakJhYzBWdlQyNUJOMmM5UFEuRDdiMG1ZWW1XRE1PbDF6eEM1SkoxRzlPQVN4NHBOOTMwMHZYTVZpTU04LUJJZ01UeHZSVHlxTUlqVHp4Zl9qVDNrZU5Xa21Wd1NRVmZqYmlvUUlueUE=

?

获取接口返回数据

我们想登录的话,必须先得到 lt 和 execution 这2个参数,那么问题来了:这两个参数是从哪来的?
打开登录页面https://account.chsi.com.cn/passport/login直接刷新,看返回的html内容






接下来我们可以用lxml爬虫etree,从html中解析出 lt 的 value 和 execution 的 value 这两个值

import requests,urllib3,os
from bs4 import BeautifulSoup
urllib3.disable_warnings()
from lxml import etree

#初始化会话
s = requests.Session()

def get_execution_it():
result = {}
url = "https://account.chsi.com.cn/passport/login"

# 登录get方法,不用传入cookie
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}
  
s.headers.update(header)
r = s.get(url,verify=False)
# print(r.content.decode("utf-8"))
html = etree.HTML(r.content.decode("utf-8"))
# print(html)
# tag = html.find(class_="cr_top_my")
# print(len(tag))

try:

result["lt"] = html.xpath('//input[@name="lt"]')[0].get("value")
result["execution"] = html.xpath('//input[@name="execution"]')[0].get("value")
print(result)

except:
print("获取1t/execution失败")
return result


if __name__ == '__main__':
result = get_execution_it()

JSESSIONID

登录里面实际上会有一个非常重要的cookies参数,JSESSIONID=A7E6B911CD6E59123F657784E5BDD7B3,这个JSESSIONID也是动态的,每次重新打开页面都会变。
这个参数也是第一次访问登录页面时候,服务器会自动返回过来的,使用浏览器无痕模式首次访问就能抓取到了。

?

?

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

?

?

?

参考代码:

import requests,urllib3,os
from bs4 import BeautifulSoup
urllib3.disable_warnings()
from lxml import etree

#初始化会话
s = requests.Session()

def get_execution_it():
result = {}
url = "https://account.chsi.com.cn/passport/login"

# 登录get方法,不用传入cookie
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}

s.headers.update(header)
r = s.get(url,verify=False)
# print(r.content.decode("utf-8"))
html = etree.HTML(r.content.decode("utf-8"))
# print(html)
# tag = html.find(class_="cr_top_my")
# print(len(tag))

try:

result["lt"] = html.xpath('//input[@name="lt"]')[0].get("value")
result["execution"] = html.xpath('//input[@name="execution"]')[0].get("value")
print(result)

except:
print("获取1t/execution失败")
return result

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


if __name__ == '__main__':
result = get_execution_it()
# login(result,user="自己学信网的账号",passwd="自己学信网的密码")









?

$flag 上一页 下一页