一、post請求有兩種方法傳json參數:
- 1.傳json參數(自動轉 json )
- 2.傳data參數(需 json 轉換)
代碼參考:
payload = {
"Jodie":"How are you ?",
"python":123,
"requests":True
}
url = "http://httpbin.org/post"
#第一種直接傳 json 參數(推薦使用這種)
r1 = requests.post(url, json=payload) # json 參數直接自動傳 json
print(r1.text)
print("---------------------------------------")
#第二種傳 data 參數,需要轉 json
r2 = requests.post(url, data=json.dumps(payload)) #傳 data 參數就需要傳 json
print(r2.text)
運行后的打印結果:
F:\test-req-py\venv\Scripts\python.exe F:/test-req-py/day3/t3.py
{
"args": {},
"data": "{\"Jodie\": \"How are you ?\", \"python\": 123, \"requests\": true}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "59",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": {
"Jodie": "How are you ?",
"python": 123,
"requests": true
},
"origin": "223.104.210.143, 223.104.210.143",
"url": "https://httpbin.org/post"
}
---------------------------------------
{
"args": {},
"data": "{\"Jodie\": \"How are you ?\", \"python\": 123, \"requests\": true}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "59",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": {
"Jodie": "How are you ?",
"python": 123,
"requests": true
},
"origin": "223.104.210.143, 223.104.210.143",
"url": "https://httpbin.org/post"
}
Process finished with exit code 0
二、快遞查詢案例
1.例如打開快遞網:http://www.kuaidi.com/,搜索 75135471609813 單號,判斷它的狀態是不是已簽收
實操中原本的理想代碼,實際沒不通:
#coding:utf-8 import requests import json url = "http://www.kuaidi.com/index-ajaxselectcourierinfo-75135471609813-zhongtong.html" h = { "Connection": "keep-alive", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/70.0.3538.110 Safari/537.36", "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8", "Referer":"http://www.kuaidi.com/", "Cookie":"lang=zh-cn; theme=default; sid=ngr6ji5prpj85cmj7lncnccap7; " "UM_distinctid=169c4b016739f-069c297f9ff0e7-3a3a5d0c-100200-169c4b016752ec; " "CNZZDATA1254194234=2076602214-1553782570-%7C1553782570" } d= { "geetest_challenge":"af4536ec53cb7935a0326b1c4031f1a0jt", "geetest_validate":"49aec4801b8b9a125fa1473876b2f036", "geetest_seccode":"49aec4801b8b9a125fa1473876b2f036|jordan" } s = requests.session() r = s.post(url, headers=h, data=d, verify=False) result = r.json() print(type(result)) print(result["company"]) data = result["data"] print(data) print(data[0]) get_result = data[0]['context'] print(get_result) if u"簽收" in get_result: print("快遞單已簽收成功") else: print("未簽收")
運行后 print(result["company"]) 返回的結果為 None ,后面的返回錯誤:
一 一排查沒找到原因,直到在 fiddle 中用 Composer 發送請求后,查看返回后的結果才明白,具體如下:
1.用composer發送請求(將之前發送成功的請求拖至此處)
2.查看返回的結果
實際上這個接口在fiddler上都沒有跑通,用如下代碼驗證:
#coding:utf-8
import requests
import json
# import urllib3
# urllib3.disable_warnings()
url = "http://www.kuaidi.com/index-ajaxselectcourierinfo-75135471609813-zhongtong.html"
h = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/70.0.3538.110 Safari/537.36",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Referer":"http://www.kuaidi.com/",
"Cookie":"lang=zh-cn; theme=default; sid=ngr6ji5prpj85cmj7lncnccap7; "
"UM_distinctid=169c4b016739f-069c297f9ff0e7-3a3a5d0c-100200-169c4b016752ec; "
"CNZZDATA1254194234=2076602214-1553782570-%7C1553782570"
}
d= {
"geetest_challenge":"af4536ec53cb7935a0326b1c4031f1a0jt",
"geetest_validate":"49aec4801b8b9a125fa1473876b2f036",
"geetest_seccode":"49aec4801b8b9a125fa1473876b2f036|jordan"
}
s = requests.session()
r = s.post(url, headers=h, data=d, verify=False)
result = r.json()
print(type(result))
print(result["company"])
print(result["companytype"])
print(result["reason"])
返回的結果如下:
與 fiddler 返回的結果一致
所以是這個接口再次請求的時候就出現問題了,有興趣可以再換一個近期的快遞單號試試
本次實操收獲:以后在做接口測試的時候盡量先在fiddler 的 Compser上請求一次,看下能不能先跑通,以免出現問題的時候花大量的時間來排查!!