問(wèn)題
近期抓數(shù)據(jù)時(shí)發(fā)現(xiàn)一個(gè)接口怎樣都無(wú)法獲取數(shù)據(jù),研究請(qǐng)求包發(fā)現(xiàn)了幾處了其他請(qǐng)求不一樣的地方
Content-Type 的值是 multipart/form-data
image
image
Google 之后找到了正確的請(qǐng)求方式
方案
首先構(gòu)建 headers 時(shí)不要定義 Content-Type;具體原因不太清楚,我是在注釋掉 Content-Type 才跑通了代碼。
首先定義 post 參數(shù)
form_data = {"name":"maka","adult":True}
抓包的數(shù)據(jù)中,傳遞的是 json 數(shù)據(jù),adult 的值是 true,直接粘貼到 Python 中會(huì)出錯(cuò),需要將 true 改為 True;false、null 同理
files = {'form_data':(None,json.dumps(form_data))}
req = requests.post(url=url,files=files,headers=headers) #發(fā)送請(qǐng)求
req_text = req.text #獲取數(shù)據(jù)信息
result = json.loads(req_text) #將數(shù)據(jù)轉(zhuǎn)為 json 格式