【问题标题】:How to post files to a multipart/form-data?如何将文件发布到多部分/表单数据?
【发布时间】:2014-03-24 15:33:46
【问题描述】:

我想上传文件并将其提交到http://www.broadinstitute.org/cmap/newQuery?servletAction=querySetup&queryType=quick 上的表单(需要登录)。表格如下

<form name="form1" enctype="multipart/form-data" method="post" action="newQuery">
    <input type="hidden" name="servletAction" value="quickQuery">
        <div class="formTable">
            <div class="row">
                <span class="label" style="width: 100px;">up tag file:</span>
                <span class="field"><input type="file" name="ups" size="30" accept="grp"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size="-1"><a href="#" onClick="window.open('help_topics_frames.jsp?topic=tag list', 'helpTopicsWindow', 'scrollbars,resizable,height=600,width=700')">tag file help</a></font></span>
            </div>
            <div class="row">
                <span class="label" style="width: 100px;">down tag file:</span>
                <span class="field"><input type="file" name="dns" size="30" accept="grp"></span>
            </div>
            <div class="row">
                <span class="label" style="width: 100px;">&nbsp;</span>
                <span class="navigation"><input type="button" onClick="submitForm()" name="submitButton" value="execute query"></span>
            </div>
        </div>
</form>

首先,从答案https://stackoverflow.com/a/22547541/651779我得到带有登录凭据的cookie

import http.cookiejar
import urllib
from bs4 import BeautifulSoup
import requests

submit_signature_url = 'http://www.broadinstitute.org/cmap/newQuery?servletAction=querySetup&queryType=quick'
login_url = 'http://www.broadinstitute.org/cmap/j_security_check'

login_values = urllib.parse.urlencode({'j_username': 'example',
                                       'j_password': 'example', 
                                       'submit': 'sign in'})

payload_submit_signature = bytes(login_values, 'ascii')

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(
    urllib.request.HTTPRedirectHandler(),
    urllib.request.HTTPHandler(debuglevel=0),
    urllib.request.HTTPSHandler(debuglevel=0),
    urllib.request.HTTPCookieProcessor(cj))

resp_1 = opener.open(submit_signature_url) #First call to capture the JSESSIONID
resp = opener.open(login_url, payload_submit_signature)

这可以正常工作。现在我想将文件发布到表单。我尝试使用这个答案https://stackoverflow.com/a/12385661/651779

# changed after Brett Lempereur's answer
values = {'ups':open(r'path\to\up.grp','rb'),
          'dns':open(r'path\to\down.grp','rb')}

submit_signature_url = 'http://www.broadinstitute.org/cmap/newQuery?servletAction=querySetup&queryType=quick'
req = requests.post(submit_signature_url, files=values, cookies=cj)
soup = BeautifulSoup(req.text)
print(soup.prettify())

这会打印出来

Requested URL:  http://www.broadinstitute.org/cmap/newQuery
java.lang.NullPointerException

如果您已登录并在浏览器中转到http://www.broadinstitute.org/cmap/newQuery,您会得到相同的结果。如果我在 requests.post:req = requests.post(submit_signature_url, data=values, cookies=cj) 中使用 data 而不是 files,它会打印包含表单的页面的 html,因此它不会发布表单。

如何发布到 multipart/form-data?


对于一个示例up-file,将以下内容复制到一个文件中并调用它 up.grp

205258_at
221369_at
205751_at
212954_at
219914_at
206703_at
207352_s_at
203548_s_at
203549_s_at
210382_at
212110_at
213805_at
213935_at
218739_at
219737_s_at
219738_s_at
204131_s_at
204132_s_at
210655_s_at
217399_s_at
206791_s_at
206792_x_at
211818_s_at
221523_s_at
221524_s_at

对于一个示例文件,将以下内容复制到一个文件中并将其命名为 down.grp

204725_s_at
211063_s_at
219921_s_at
200618_at
219701_at
220342_x_at
220926_s_at
201323_at
204692_at
221956_at
222017_x_at
90610_at
217755_at
207843_x_at
209366_x_at
215726_s_at
201827_at
201185_at
212411_at
201692_at
214484_s_at
202910_s_at
202381_at
200663_at
201459_at
219770_at
220853_at
201349_at
207015_s_at
207016_s_at
212338_at
220330_s_at

【问题讨论】:

  • 向上或向下输入都没有name。它们是由 JS 还是表单上的东西填充的?
  • @Drewness 啊,我把他们的名字删掉了。他们确实有名字upsdns。我在问题中添加了它。
  • 是的,我想通了。我的“明显”猜测出现了……

标签: javascript python html post multipartform-data


【解决方案1】:

为了冒险猜测,当前版本的 requests 模块期望其参数具有以下格式之一:

files = {"name": file-handle, ...}
files = {"name": (file-name, file-handle, content-type,), ...}

您是否尝试过将您创建的文件字典替换为以下内容:

values = {'ups': open(r'path\to\up.grp', 'rb'), 'dns': open(r'path\to\down.grp', 'rb')}

您的代码在技术上是正确的,但在语义上并不正确,因为 some 文件数据将被发送到站点,但它实际上是字符串文字 r'path\to\up.grp 的内容'。

通过阅读表单,您还应该将字典{"servletAction": "quickQuery"} 作为数据参数传递给requests.post 调用。此外,您发布到的 URL 应该是 http://www.broadinstitute.org/cmap/newQuery,没有您在原始代码中使用的当前查询字符串。

【讨论】:

  • 这给出了相同的Requested URL: http://www.broadinstitute.org/cmap/newQuery java.lang.NullPointerException 错误
  • 嗯,如果我能得到一些示例数据(即 up.grp 和 down.grp 文件),我可以在这里运行一些测试。您肯定要进行修复,否则您将永远不会发送文件的实际内容,但似乎错误的根源在其他地方。
  • 我能想到的唯一另一件事是提交一个带有 data={"servletAction": "quickQuery"} 的值字典以及您的文件。
  • 最后一条评论修复了它,谢谢!你能把它放在你的答案中以防其他人需要吗?
猜你喜欢
  • 2015-10-31
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2016-02-12
  • 2016-11-08
  • 2014-01-08
  • 2019-07-10
相关资源
最近更新 更多