有一个网站有一个我想下载的文件列表.为了简化这个过程,我尝试编写一个脚本来为我完成. (即使我可以同时选择多个选项,单击“提交”仅下载第一个文件)
网页网址,网页/ list.php与表单中的操作不同.我不确定在这里发布实际网址的政策是什么.
Downloadable file1 Downloadable file2 Downloadable file3 |
我的脚本是这样的:
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重定向问题?
这是我使用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
本文介绍如何使用Python脚本通过POST方法从网页表单下载文件。示例代码展示了如何设置请求参数并保存文件,但遇到问题:服务器响应的是文件列表页面而非文件本身。问题可能涉及重定向或表单数据处理。

被折叠的 条评论
为什么被折叠?



