2017-06-16 73 views
0

當我發出POST請求時,我無法從網站獲得有用的響應。POST多部分編碼文件python

import requests 

data = {'image': ("biloha.jpg", open("biloha.jpg", "rb"), 'image/jpeg')} 
r = requests.post("http://basic.photofunia.com/categories/all_effects/worker-by-the-billboard/", 
         data=data) 
print(r.url) 

我得到的迴應: http://basic.photofunia.com/categories/all_effects/worker-by-the-billboard?e=resource_not_found

,但我一定要得到這樣的事情:

Server: "nginx" 
Date: "Fri, 16]un 2017 12:39:12 GMT" 
Content-Type: "text/html" 
Transfer-Encoding: "chunked" 
Connection: "close" 
Pragma: "no—cache" 
X-Powered-By: "PFEngine/13" 
Cache-Control: "no—store" 
Expires: "Mon, 01 Jan 1997 12:00:00 GMT" 
Location: "/results/5943d170846d7843628b45c7" 
+1

歡迎來到StackOverflow。請勿在外部託管圖片,請勿拍攝/張貼文字圖片。我已經提前完成並對您的帖子進行了編輯,以便用文本替換文本圖片。 –

回答

0

用於發送多形式的數據,你必須通過文件參數,使文件能上傳。

import requests 
import sys 

data = {'image': ("biloha.jpg", open(sys.argv[1], "rb"), 'image/jpeg')} 
r = requests.post("http://basic.photofunia.com/categories/all_effects/worker-by-the-billboard/", 
         files=data) 
print(r.url) 
+0

謝謝,它的工作原理! –