当我将-P或-O单独使用时wget,一切都如广告中所述。
$: wget -P "test" http://www.google.com/intl/en_com/images/srpr/logo3w.png
Saving to: `test/logo3w.png'
Run Code Online (Sandbox Code Playgroud)
。
$: wget -O "google.png" http://www.google.com/intl/en_com/images/srpr/logo3w.png
2012-01-23 21:47:33 (1.20 MB/s) - `google.png' saved [7007/7007]
Run Code Online (Sandbox Code Playgroud)
但是,结合这两个原因wget可以忽略-P。
$: wget -P "test" -O "google.png" http://www.google.com/intl/en_com/images/srpr/logo3w.png
2012-01-23 21:47:51 (5.87 MB/s) - `google.png' saved [7007/7007]
Run Code Online (Sandbox Code Playgroud)
我为目录(由URL的最后一个块生成)和文件名(通过计数循环生成)都设置了一个变量,使http://www.google.com/aaa/bbb/cccyields file= /directory/filename,或者对于项目1,/ccc/000.jpg
将其替换为代码时:
Popen(['wget', '-O', file, theImg], stdout=PIPE, stderr=STDOUT)
wget静默失败(在循环的每次迭代中)。
当我打开调试-d和日志记录时-a log.log,每次迭代都会打印
DEBUG output created by Wget 1.13.4 on darwin10.8.0.
当我删除-O和时file,操作将正常进行。
我的问题是:有没有办法
A)在(首选)或
B)中都指定-P AND -O,将一个字符串插入到- 不会导致失败的字符中?wget-O/
任何帮助,将不胜感激。
wget.download(..)的文档:
def download(url, out=None, bar=bar_adaptive):
"""High level function, which downloads URL into tmp file in current
directory and then renames it to filename autodetected from either URL
or HTTP headers.
:param bar: function to track download progress (visualize etc.)
:param out: output filename or directory
:return: filename where URL is downloaded to
"""
...
Run Code Online (Sandbox Code Playgroud)
使用以下调用将文件下载到具有自定义文件名的特定目录(已经存在):
wget.download(url, path_to_output_file)
Run Code Online (Sandbox Code Playgroud)
如果您希望函数调用抽象出目录创建(如果尚不存在),请使用:
urllib.urlretrieve(url, path_to_output_file)
Run Code Online (Sandbox Code Playgroud)
你应该直接传递dir/000.jpg给-Oof wget:
import subprocess
import os.path
subprocess.Popen(['wget', '-O', os.path.join(directory, filename), theImg])
Run Code Online (Sandbox Code Playgroud)
从你的问题中尚不完全清楚你是否已经在做类似的事情,但如果你做了并且仍然失败了,我可以想到两个原因:
的参数-O包含一个前导/,导致wget失败,因为它没有在/(root) 中随机创建目录的权限。
您要写入的目录wget不存在。os.mkdir您可以通过首先在 Python 标准库中创建它来确保它存在。
您还可以尝试从调用中删除参数stdout=和 ,以便可以直接查看错误,或使用 Python 打印它们。stderr=Popen
| 归档时间: |
|
| 查看次数: |
8852 次 |
| 最近记录: |