2013-03-26 79 views
3

有一個網站有一個我想要下載的文件列表。爲了緩解這個過程,我嘗試寫一個腳本來爲我做。 (即使我可以同時選擇多個選項,單擊提交只會下載第一個文件)通過郵政形式下載文件

網頁url,webpage/list.php與表單中的操作不同。我不確定在此處發佈實際網址時的政策是什麼。

<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() 

當運行該腳本的服務器似乎並沒有確認該請求,只是提供給我的文件列表相同的網頁。有沒有我缺少的選項?可能有索姆重定向問題?

這是信息使用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 

回答

5

你應該考慮使用「請求」庫:http://docs.python-requests.org/en/latest/

我相信這會是這樣使用的要求庫來完成:

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

行:

req = urllib2.Request('webpage/list.php') 

請求的原始網頁,但形式職位數據webpage/data.php。這是否更好?

req = urllib2.Request('webpage/data.php')