9 介面自動化之requests 第三方庫

2021-10-09 01:24:57 字數 1520 閱讀 1744

一、簡單的request請求

#未傳cookie

import requests

# 型別按照駝峰形式抒寫

class login:

"""利用requests封裝 get post請求"""

def http_request(self, url, data, method, cookie=none): # 引數化

"""url請求的位址 http://***x:port"""

"""param:傳遞的引數 非必填引數 字典的格式傳遞引數"""

"""method:請求方法 支援get 以及post """

#lower()小寫識別

if method.lower() == "get":

# verify=false忽略證書 ssl

res = requests.get(url, data, cookies=cookie, verify=false)

return res

elif method.lower() == "post":

res = requests.post(url, data, cookies=cookie, verify=false)

return res # 返回的是乙個訊息實體

else:

raise exception('不支援的請求')

#html格式

print("響應正文1:",res.text)

print("響應狀態碼:",res.status_code)

print("響應頭:",res.headers)#響應頭

print("**user-agent",res.request.headers)#請求頭

return res #返回的是乙個訊息實體

if __name__ == '__main__':

url = ""

#將**偽裝成遊覽器,user-agent是請求頭裡面的資料

header=#偽裝的chrome遊覽器

# 輸出格式為json格式

result = res_login.json()

print("請求返回的結果為:",result)

#注:insecurerequestwarning只是不安全的意思

#登入成功才會有cookie

#html xml json ---》text

#html xml json ---》json()會報錯 只有json的返回值才支援json

#1、遮蔽它

#2、搞個萬能的

#3、去資料庫裡面查實時的

#4、手動填,比如發在手機上的驗證碼

#請求頭裡面包含的資料:accept-language、accept-charset、accept、accept-encoding、user-agent、connection

Excel介面自動化(8)介面測試自動化指令碼

介面測試自動化指令碼 整個流程的邏輯基本都是在這裡面實現,所需要的資料都是通過呼叫前面的封裝來獲取 第一步 新建乙個解析excel工具類的例項物件並且獲取 api 的sheet物件 parsee parseexcel parsee.loadworkbook filepath sheetobj par...

python之requests介面自動化

在進行介面自動化測試時,有好多介面都基於登陸介面的響應值來關聯進行操作的,在次之前試了很多方法,都沒有成功,其實很簡單用session來做。coding utf 8 import requests 在登陸模組建立乙個全域性session,在其他介面操作時帶入登陸時的session,保持session...

介面自動化之 requests詳解

1.什麼是requests?答 requests是用來傳送http請求以及接收http響應的python第三方庫,主要用於做介面自動化。2.安裝 pip install requests 3.requests常用方法 requests.get url,params none,kwargs kwarg...