在subprocess中使用wget
我正在尝试用 subprocess
来使用 wget。
我之前的尝试都很顺利,直到我想用下面的代码把网页下载到指定的文件夹:
url = 'google.com'
location = '/home/patrick/downloads'
args = ['wget', 'r', 'l 1' 'p' 'P %s' % location, url]
output = Popen(args, stdout=PIPE)
如果我在 /home/patrick
这个文件夹下运行这段代码,我得到的 index.html
文件会出现在 /home/patrick
里,而不是 /home/patrick/downloads
里。
你能帮我吗?
谢谢;)
2 个回答
0
编辑: popen
是来自 os
的一个功能,它的目的是要被 替代 的 os.popen
模块。因此,不建议使用 os.popen
。
最开始我以为这是 os
里的 popen
。
如果你在使用 os
里的 popen
#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))
并且在使用 subprocess
里的 Popen
#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)
如果我漏掉了什么,请告诉我。
谢谢!
4
你需要使用连字符,并且 location
应该只是另一个参数:
args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]