将wget与子进程一起使用

2024-06-08 15:22:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将wget与subprocess一起使用。

在尝试使用以下代码将页面下载到指定目录之前,我的尝试一直有效:

url = 'google.com'
location = '/home/patrick/downloads'
args = ['wget', 'r', 'l 1' 'p' 'P %s' % location, url]

output = Popen(args, stdout=PIPE)

如果我在/home/patrick中运行此代码,我会在/home/patrick中得到index.html,而不是在/home/patrick/downloads中。

你能帮我吗?

谢谢;)


Tags: 代码目录comurlhomeoutputdownloadsgoogle
2条回答

您需要有连字符,location应该只是另一个参数:

args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]

来自os的编辑:popen打算replaceos.popen模块。因此,不建议使用os.popen

最初我以为是来自ospopen

如果您使用popenfrom os

#wget 'http://google.com/' -r -l 1 -p -P /Users/abhinay/Downloads

from os import popen

url = 'google.com'
location = '/Users/abhinay/Downloads'
args = ['wget %s', '-r', '-l 1', '-p', '-P %s' % location, url]

output = popen(' '.join(args))

使用Popen来自subprocess

#wget 'http://google.com/' -r -l 1 -p -P Downloads/google

from subprocess import Popen

url = 'google.com'
location = '/Users/abhinay/Downloads'
#as suggested by @SilentGhost the `location` and `url` should be separate argument
args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]

output = Popen(args, stdout=PIPE)

如果我遗漏了什么,请告诉我。

Thx!

相关问题 更多 >