Python 将wget与子流程一起使用
我正在尝试将wget与Python 将wget与子流程一起使用,python,subprocess,Python,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中运行此代码,我会在/home/patrick中得到index.htm
子流程一起使用
我的尝试一直有效,直到我尝试使用以下代码将页面下载到指定目录:
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
中
你能帮我吗
谢谢;) 您需要有连字符,location
应该只是另一个参数:
args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]
您需要有连字符,location
应该只是另一个参数:
args = ['wget', '-r', '-l', '1', '-p', '-P', location, url]
编辑:popen
fromos
打算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))
并使用子流程中的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)
如果我遗漏了什么,请告诉我
谢谢 编辑:popen
fromos
打算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))
并使用子流程中的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)
如果我遗漏了什么,请告诉我
谢谢 还有一些空间?所有这些单引号字符串都将被混合在一起,它们之间没有空格。现在它们之间有了逗号,我认为这会起作用。在Patrick发布的代码中,那些没有逗号的元素使它们成为数组中的独立元素,所有的gget都连接在一起。你的回答是正确的,傻鬼。是的!谢谢它的工作原理是:D我只是忘了在“l1”和“p”之后加上一些逗号;)还有一些空间?所有这些单引号字符串都将被混合在一起,它们之间没有空格。现在它们之间有了逗号,我认为这会起作用。在Patrick发布的代码中,那些没有逗号的元素使它们成为数组中的独立元素,所有的gget都连接在一起。你的回答是正确的,傻鬼。是的!谢谢它的工作原理是:D我只是忘了在“l1”和“p”之后加上一些逗号;)文档说:args应该是一个字符串或一系列程序参数。应该使用subprocess.Popen而不是os。popen@SilentGhost起初我以为是os的popen,现在我编辑了我的答案@科里:谢谢你的建议!看起来popen
是一个较旧的版本,被popen
取代。天哪,我甚至无法想象你会建议os。打开,它确实需要一个字符串作为第一个参数。文档说:args应该是一个字符串,或者应该使用程序参数序列.subprocess.Popen而不是os。popen@SilentGhost起初我以为是os的popen,现在我编辑了我的答案@科里:谢谢你的建议!看起来popen
是一个更老的版本,被popen
取代了。天哪,我甚至无法想象你会建议os。打开,它确实需要一个字符串作为第一个参数。为什么不使用urllib或httplib?我认为不使用python库的主要原因是,如果你得到的文件很大,你不必使用解释器内存来下载和处理它们-你只需将它交给另一个为完成这一部分而构建的进程,我需要一个命令来下载整个网页:)为什么不使用urllib或httplib?我认为不使用python库的主要原因是,如果你得到的是大文件,你不必使用解释器内存来下载和处理它们-你只需将它交给另一个为此而构建的进程,我需要一个命令来下载整个网页:)