我有一个通过 subprocess.Popen 调用 wget 的函数。此函数的目的是生成 wget 并抓取一个网站以获取链接列表。
是否可以判断 wget 进程何时完成,然后继续执行 python 函数的其余部分,例如
def get_urls(url, uname, pword, output):
subprocess.Popen (['wget', '-nd', '-r', '--user=', uname, '--password=', pword,
'--no-parent','--spider',url, '--output-file= ',output], stdout=subprocess.PIPE)
#some method telling wget has finished writing to the output file, so continue
foo = bar() #rest of function etc.
是否还有更好的方法通过 python 抓取站点(并传递登录凭据)而不是进行系统调用?
谢谢
为什么要使用子进程,也许使用 urllib 更好
import urllib
url = 'http:......'
filename = 'your_filename'
urllib.urlretrieve(url, filename)
当您调用 subprocess.Popen 时,它会创建一个在后台运行的新进程,并且代码继续运行。
如果你想等待创建的进程完成,只需使用 Popen.wait 方法。
new_process = subprocess.Popen(...)
new_process.wait() # waits for the process to finish. you can also pass a timeout parameter
foo_bar() # do whatever after download is finished