Python_requests实例
在测试过程中,需要进行团体报名,需要测试报名的最高上限数量,以及分页,若一个个进行报名,很浪费时间和精力,我们想到使用python语言编写代码来实现。
import requests
header ={
"authorization": "cbafcba131318a4112f82743c8b08f0b3f655cd1",
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15"
}
def apply(name, cardNumber):
applyData = {
"pName": name,
"gender": 1,
"country": 344,
"identType": 2,
"identCard": cardNumber,
"birthDate": "1993-06-03",
"phone": "100000000",
"email": "x@b.am",
"bloodType": "A",
"province": "上海市",
"city": "黄浦区",
"Address": "ceshi",
"emergency": "dd",
"emergencyPhone": "15811540163",
"job": 1,
"education": 1,
"income": "5万以内",
"matchEventID": "5192974392788471",
"teamID": 5210251418835258,
"runnerSource": 2
}
applyUrl = "https://saas-user-gw-test.mararun.com/v1/application/apply"
try:
request = requests.post(url = applyUrl, headers=header,data = applyData)
print("status_code = %s" %request.status_code)
print(request.json())
except Exception as e:
print(e)
for i in range(100,152):
print("i=",i)
name = "testName"+ str(i)
cardNumber = i
apply(name, cardNumber)
print("-----------------")
实例2:
import requests import json import urllib3 urllib3.disable_warnings() def ware_detail(): url='http://detail.dmall.com/app/wareDetail/extendinfo' header = { "Host": "detail.dmall.com", "device": "iPhone11,8", "deliveryLat": "39.720337", "appName": "com.dmall.dmall", "storeGroupV4": "", "dev_type": "iPhone", "platform": "IOS", "smartLoading": "1", "areaId": "653024", "Accept": "*/*", "isOpenNotification": "0", "wifiState": "1", "ticketName": "647C246592F7BA3A1984BEC4D31482D82C385615E45EF045BF6CD2A23A91757C0689CC1EFC6972D40A87D4360B82132371F66E8DA9F830DD5E96A770AF04249C8454F305B2FCF1BF2484BAAB390433742F45E19ACCAA83183E63D63ACFD270037178FBDE0723FE7BBDEA77C4BC7F46D487AC1071008F5204B56247107617B13D", "screen": "896*414", "sessionId": "28994A6674D647F8A4EB8A8740D33723", "User-Agent": "dmall/4.8.1 (iPhone; iOS 13.6.1; Scale/2.00)", "businessCode": "2", "channelId": "APPSTORE", "xyz": "ac", "storeId": "63", "version": "4.8.1", "uuid": "95adf89d52e7c71b86c8313aeb2f9796a8507241", "apiVersion": "4.8.0", "deliveryLng": "75.252452", "Content-Length": "167", "Accept-Encoding": "gzip, deflate, br", "venderId": "1", "currentTime": "1599825826829", "storeGroupKey": "a33c9857bc257732c342b727284b8f79@MS0xODAxNS02", "networkType": "0", "tpc": "category_65372", "Accept-Language": "zh-Hans-CN;q=1", "sysVersion": "13.6.1", "lng": "116.320215", "idfa": "29B52893-C109-425E-A3D4-543AEC01B2B7", "platformStoreGroupKey": "a92582e6317aea113088bf2f915ffc3d@MjQ1LTE3MjY4", "env": "app", "cid": "cbb913b31c0705a9a2464cbeac515e2c", "recommend": "1", "dev_carrier": "", "Content-Type": "application/x-www-form-urlencoded", "lat": "39.976486", "Connection": "keep-alive", "token": "66011cec-a131-4969-ab04-11a846356950", "platformStoreGroup": "", "appMode": "online", "userId": "163863113"} post_param = {"lng":116.320725,"lat":39.976565000000001,"wareVenderId":"1","wareStoreId":"112","sku":"100822811"} post_param = "param=%s" %json.dumps(post_param, ensure_ascii=False) #json.dumps 序列化时对中文默认使用的ascii编码.想输出真正的中文需要指定ensure_ascii=False r = requests.post(url=url,headers = header,data = post_param,verify = False) print(r.json())