2014-06-17 30 views
2

我想通过python 2.7使用http://www.camp.bicnirrh.res.in/featcalc/发布multipart/form-data。具体来说,我在FASTA格式上传文件(称为'Practice.txt')基本上是这样的格式:使用request.post通过python发布多部分表单数据不工作

'>1(ENTER)STRINGOFSPECIFICCAPITALLETTERS' 

这个网站里面也有一个文本框,您也可以手动输入数据(我要离开的空白) 。此数据网站也有复选框选项,其中我要选择'Length','Net Charge''Aliphatic Index''Hydrophobicity'。在页面底部有一个'Submit'按钮。
目前,这是我用于我的POST响应的代码。

files = {'file': ('Practice.txt', open('Practice.txt', 'rb'))} 
data = {'Length':'Length', 'Net Charge':'Net Charge', 'Aliphatic Index':'Aliphatic Index','Hydrophobicity':'Hydrophobicity'} 
r = requests.post(url, files=files, data=data) 
r.text 

问题是,当我做r.text时,没有任何数据会回来。网站在使用浏览器时计算所有这些东西的值。我得到了WireShark,我一直试图查看实时提要,看看我发送给服务器的是什么,虽然我使用的是逐字以上的代码,但它并没有回报浏览器的值。

有没有人有任何想法,为什么这可能会发生/如何实际获取数据?感谢您的任何意见!

回答

3

这将工作:

import requests 
import urllib 

session = requests.Session() 

file={'file':(open('practice.txt','r').read())} 

url = 'http://www.camp.bicnirrh.res.in/featcalc/tt1.php' 

payload = { 
    'length' :'length',  #Length 
    'netcharge':'netcharge', #Net Charge 
    'aliphatic':'aliphatic', #Aliphatic Index 
    'gravy' :'gravy'  #Hydrophobicity 
    } 

raw = urllib.urlencode(payload) 

response = session.post(url, files=file, data=payload) 

print(response.text) 

所有选项:

payload = { 
    'length'  :'length',  #Length 
    'netcharge' :'netcharge', #Net Charge 
    'amino'  :'amino',  #Amino acid composition 
    'aliphatic' :'aliphatic', #Aliphatic Index 
    'instability':'instability', #Instability Index 
    'gravy'  :'gravy',  #Hydrophobicity 
    'sec'  :'sec'   #Secondary Structure Propensity 
    } 
+0

太谢谢你了!这工作!对此,我真的非常感激! – Shay

+0

@Shay很高兴能有帮助!如果您觉得这对您有用,请随时接受我的回答。 :-)(http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) – NorthCat

+0

再次感谢之前。我希望你可以再次帮我解决另一个POST请求,这个请求应该和上面的一样,但是由于某种原因,它不是。我做了上面的所有细节 - 导入请求,导入urllib,session = requests.Session(),file = {'file':(打开('Bishop/newdenovo2.txt','r')。read())} ,url ='http://www.camp.bicnirrh.res.in/predict/hii.php'payload = {“algo []”:“svm”}(因为我只想要第一个复选框项目'SVM')原始和响应与上面的相同。我也可以将其作为一个问题发布!再次感谢你! – Shay

相关问题