【问题标题】:Using request.post to post multipart form data via python not working使用 request.post 通过 python 发布多部分表单数据不起作用
【发布时间】:2014-06-17 04:36:10
【问题描述】:

我正在尝试使用 http://www.camp.bicnirrh.res.in/featcalc/ 通过 python 2.7 发布 multipart/form-data。具体来说,我正在上传FASTA 格式的文件(称为'Practice.txt'),基本上是这种格式:

'>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,我一直在尝试查看实时提要,以查看我发送到服务器的确切内容,尽管我逐字使用上面的代码,但它并没有返回浏览器会。

有没有人知道为什么会发生这种情况/如何实际获取数据?感谢您的任何意见!

【问题讨论】:

    标签: python post request multipartform-data


    【解决方案1】:

    这将有效:

    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
      }
    

    【讨论】:

    • 非常感谢!这行得通!我真的很感激!
    • @Shay 很高兴能帮上忙!如果您觉得我的回答对您有用,请随时接受。 :-) (meta.stackoverflow.com/questions/5234/…)
    • 再次感谢您。我希望你能再一次帮助我处理另一个应该与上述相同的 POST 请求,但由于某种原因它不是。我做了上面的所有事情都做了一些细微的改变——导入请求、导入 urllib、会话 = requests.Session()、file={'file':(open('Bishop/newdenovo2.txt','r').read())} , url = 'camp.bicnirrh.res.in/predict/hii.php' payload = {"algo[]":"svm"} (因为我只想要第一个复选框项目'SVM'),原始和响应与上面的相同。我也可以将此作为问题发布!再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多