Python Subprocess:我做错了什么?

时间:2017-12-09 20:30:42

标签: python python-3.x automation subprocess wget

概述:我正在尝试使用Python子进程模块来执行复杂的wget命令。我正在使用Python,因为我可以在每个作业的HTTP标头值之间随机切换,更好地组织结果等。我喜欢wget而不是类似的Python包,因为我知道wget非常好并且更喜欢它。我也喜欢通过翘曲将作业通过Tor继电器进行布线是多么容易。作为参考,我是Ubuntu用户。我非常感谢你的帮助。

问题:在子流程模块方面经验不足,我花了很多时间在Python.org和Stackoverflow上学习如何正确使用子流程。但是,我似乎无法将我在那里学到的知识与我的问题解决方案联系起来。在某些情况下,Python会正确地创建时间标记结果目录,但我会在其他地方找到结果,比如pwd。在其他情况下,目录创建有效,但wget函数似乎失败了。当我查看wget日志文件时,我不时弹出的错误是#34; Scheme missing。"此错误可能与目录创建有关。

相关代码

targetURL="https://www.wikipedia.org"
targethost="Wikipedia"
# the general wget results folder
root_dir='/home/me/Documents/wget/'
# the exact results folder based on the target URL and date/time
results_dir=root_dir+targethost+"/"+str(datetime.datetime.now().strftime("%Y-%m-%d"))
if not os.path.exists(results_dir):
    os.makedirs(results_dir)

# args is a list of all wget variables
subprocess.call(args, stdin=None, stdout=None, stderr=None, shell=False, timeout=None) 

args变量的示例值推送到子流程

['torsocks', 'wget', '-w 300', '--random-wait', '-e robots=off', '--append-output', '--directory-prefix=/home/me/Documents/wget/Wikipedia/2017-12-09', "--user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36'", "--header='Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'", "--header='Accept-Language: en-ZA,en;q=0.5'", "--header='Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0'", "--referer='https://www.google.com/'", "--header='Connection: keep-alive'", '-p', '-k', '-m', 'https://www.wikipedia.org/']

0 个答案:

没有答案