python接口自動化26-參數關聯和JSESSIONID(上個接口返回數據作為下個接口請求參數)


前言

參數關聯是接口測試和性能測試最為重要的一個步驟,很多接口的請求參數是動態的,並且需要從上一個接口的返回值里面取出來,一般只能用一次就失效了。
最常見的案例就是網站的登錄案例,很多網站的登錄並不僅僅只傳username和psw兩個參數,往往有其它的動態參數。
有時候還需要帶上cookies參數,如JSESSIONID

登錄參數

首先分析下目標網站【學信網:https://account.chsi.com.cn/passport/login】的登錄接口請求參數。
先隨便輸入賬號和密碼,使用fiddler工具抓包查看請求參數,用兩個參數是網頁自動給的參數(用戶沒輸入)

  • lt : LT-269530-RIkaD7y6sB6dfBwdX56cfaifqxElxx
  • execution: e1s1

關閉瀏覽器后,重復上面操作,再抓包看請求參數,會發現變了

  • lt : LT-277623-5ldGTLqQhP4foKihHUlgfKPeGGyWVI
  • execution: e1s1

備注:execution參數是表示網站刷新次數,可以刷新下再登錄,就變成 e2s1了

獲取接口返回數據

我們想登錄的話,必須先得到 lt 和 execution 這2個參數,那么問題來了:這兩個參數是從哪來的?
打開登錄頁面https://account.chsi.com.cn/passport/login直接刷新,看返回的html內容

<input class="btn_login" name="submit" accesskey="l" value="登錄" tabindex="4" type="submit" title="登錄" /> <div class="account-oprate clearfix"> <a class="find-yhm" href="https://account.chsi.com.cn/account/password!rtvlgname">找回用戶名</a> <a class="find-mm" href="https://account.chsi.com.cn/account/password!retrive">找回密碼</a> <a href="https://account.chsi.com.cn/account/preregister.action?from=account-login" class="regist-btn">注冊</a> </div> <input type="hidden" name="lt" value="LT-279621-fnisPBt0FVGNFrfWzJJqhTEyw6VkfH" /> <input type="hidden" name="execution" value="e2s1" /> <input type="hidden" name="_eventId" value="submit" /> 

接下來可以用前面學的的lxml爬蟲框架python筆記28-lxml.etree爬取html內容,從html中解析出value="LT-279621-fnisPBt0FVGNFrfWzJJqhTEyw6VkfH"和value="e2s1"這兩個值

# 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", } s.headers.update(h1) r = s.get(loginurl, verify=False) dom = etree.HTML(r.content.decode("utf-8")) try: result["lt"] = dom.xpath('//input[@name="lt"]')[0].get("value") result["execution"] = dom.xpath('//input[@name="execution"]')[0].get("value") print(result) except: print("lt、execution參數獲取失敗!") return result if __name__ == "__main__": result = get_it_execution()

運行結果:
{'lt': 'LT-286330-a6Ogf3rt3Fcwt6XZcuKCa4HHzz0QA3', 'execution': 'e1s1'}

JSESSIONID

登錄里面實際上會有一個非常重要的cookies參數,JSESSIONID=4D98C3F3ED18A2489BD17CA722D19AE8,這個JSESSIONID也是動態的,每次重新打開頁面都會變。
這個參數也是第一次訪問登錄頁面時候,服務器會自動返回過來的,使用瀏覽器無痕模式首次訪問就能抓取到了。

cookies參數關聯實現就非常簡單了,直接用requests.session()去發個get請求就能自動保存了,所以上一步get_it_execution()實際上也同步了cookies參數。

參考代碼

# 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", } s.headers.update(h1) r = s.get(loginurl, verify=False) dom = etree.HTML(r.content.decode("utf-8")) try: result["lt"] = dom.xpath('//input[@name="lt"]')[0].get("value") result["execution"] = dom.xpath('//input[@name="execution"]')[0].get("value") print(result) 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.headers.update(h2) r4 = s.post(loginurl, data=body, verify=False) print(r4.text) if __name__ == "__main__": result = get_it_execution() login(result, user='13812348888', psw='123456')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM