通过邮寄表格下载文件 [英] File download via Post form

查看:105
本文介绍了通过邮寄表格下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个网站,其中列出了我要下载的文件.为了简化此过程,我尝试编写一个脚本来替我做. (即使我可以同时选择多个选项,也可以单击提交"仅下载第一个文件)

There is a website that has a list of files that I want to download. To ease the process I tried to write a script to do it for me. (Even though I can select multiple options at the same time, clicking Submit only downloads the first file)

网页url,webpage/list.php,与表单中的操作不同.我不确定在此处发布实际网址的政策.

The webpages url, webpage/list.php, is different from the action in the form. I'm not sure about what the policy is on posting the actual url here.

<form action="webpage/data.php" method="post">
  <table align="center">
    <tbody>
      <tr><td>
        <select name="data[]" size="8" multiple="multiple">
          <option value="downloadable_file1.tar">Downloadable file1</option>
          <option value="downloadable_file2.tar">Downloadable file2</option>
          <option value="downloadable_file3.tar">Downloadable file3</option>
        </select>
      </td></tr>
    </tbody>
  </table>
  <input type="submit">
</form>

我的脚本是这样的:

import urllib
import urllib2
import shutil

req = urllib2.Request('webpage/list.php')
values = { 'data[]': 'downloadable_file1.tar'}
req.add_data(urllib.urlencode(values))
resp = urllib2.urlopen(req)

myfile = open('downloadable_file1.tar', 'wb')
shutil.copyfileobj(resp.fp, myfile)
myfile.close()

运行脚本时,服务器似乎没有确认请求,只是为我提供了与文件列表相同的网页.我有没有选择的余地?可能会有som重定向问题?

When running the script the server does not seem to acknowledge the request and just serves me the same webpage with the file list. Is there an option I am missing? Might there be som redirect problem?

这是我在使用Chrome时获得的信息:

This is the info I get when using Chrome:

Request URL:webpage/data.php  
Request Method:POST  
Status Code:200 OK  

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3  
Accept-Encoding:gzip,deflate,sdch  
Accept-Language:sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4  
Cache-Control:max-age=0  
Connection:keep-alive  
Content-Length:26  
Content-Type:application/x-www-form-urlencoded  
Host:webpage 
Origin:webpage  
Referer:webpage/list.php  
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22  
data[]:downloadable_file1.tar  

Accept-Ranges:bytes  
Cache-control:private  
Connection:Keep-Alive  
Content-Disposition:attachment; filename="downloadable_file1.tar.gz"  
Content-Length:1043436  
Content-Transfer-Encoding:binary  
Content-Type:application/x-gzip  
Date:Tue, 26 Mar 2013 20:18:58 GMT  
Expires:Mon, 26 Jul 1997 05:00:00 GMT  
Keep-Alive:timeout=5, max=100  
Pragma:private  
Server:Apache/2.2.9 (FreeBSD) mod_ssl/2.2.9 OpenSSL/0.9.7e-p1 DAV/2 PHP/5.2.6 with Suhosin-Patch
X-Powered-By:PHP/5.2.6  

推荐答案

您应该使用'requests'库进行研究: http://docs.python-requests.org/en/latest/

You should look into using the 'requests' library: http://docs.python-requests.org/en/latest/

我相信这可以使用请求库来实现:

I believe this would be accomplished like this using the requests library:

data={'data[]':'downloadable_file1.tar'}
req = requests.post('webpage/data.php', data)
with open('file.data', 'wb') as file:
    file.write(req.content)

这篇关于通过邮寄表格下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆