2014-07-17 105 views
0

我使用Python 2.7.7發送一個post請求到一個網站。使用請求模塊和我的代碼林看起來是這樣的:(NAME和PASS被取代)Python - POST請求repsonse和JSON解析

r = requests.post("http://play.pokemonshowdown.com/action.php", data="act=login&name=NAME&pass=PASS&challengekeyid="+challstrarr[2]+"&challenge="+challstrarr[3]) 

    print(r.text) 
    print(r.json()) 

r.text只返回一個空行,r.Json返回此錯誤:「ValueError異常:無JSON對象可以是解碼」

網站i'm請求具有以下教程:

you'll need to make an HTTP POST request to http://play.pokemonshowdown.com/action.php with the data act=login&name=USERNAME&pass=PASSWORD&challengekeyid=KEYID&challenge=CHALLENGE

Either way, the response will start with ] and be followed by a JSON object which we'll call data."

我不確定是否發佈請求的響應是錯誤的(因此空行),或者如果沒有故障的json解析關閉

+0

當你與捲曲嘗試會發生什麼? –

+0

我會確保'challstrarr [2]'和'challstrarr [3]'是你想象的那樣並且打印出數據來看看它的樣子 –

+0

它適用於curl,我得到json文件的內容,但我仍然與r.text空行。 我沒有檢查數組的指標,我打印出來,它的所有正確 – user3710165

回答

0

你應該通過一個字典對象到post功能(數據參數),只有在get方法你應該通過查詢字符串:

postData = { 
#put you post data here 
} 
r = requests.post("http://play.pokemonshowdown.com/action.php", data=postData) 
print(r.text) 
print(r.json()) 
+0

真棒,解決了它,謝謝! – user3710165

+0

您可以查看文檔以獲取更多詳細信息,:) – zhujs