Итак, после попытки многих различных вариантов subprocess
, это то, что работало с phantomjs
: subprocess32
!!!
import subprocess32 # not the default version; this supports timeouts
for (_id, link) in link_list:
phantomjs_call = u'{0}phantomjs {0}thumbnails.js {1} {2} {3} {4}'.format(phantomjspath, _id, link)
"""note: this generates a string like
/home/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
/home/phantomjs-2.1.1-linux-x86_64/bin/thumbnails.js 51514
"http://mysite/explore?viz=summary_text"
"""
try:
process = subprocess32.Popen(phantomjs_call, shell=True, stdout=subprocess32.PIPE)
# make sure phantomjs has time to download/process all the pages in the list
# but if we get nothing after 180 sec, just move on
except Exception as e:
print(phantomjs_call)
print('Popen failed', e)
try:
output, errors = process.communicate(timeout=180)
except Exception as e:
if debug == True:
print("\t\tException: %s" % e)
process.kill()
return "\t\tException: {0}".format(e)
# output will be weird, decode to utf-8 to save heartache
phantom_output = []
for out_line in output.splitlines():
phantom_output.append( out_line.decode('utf-8') )
Это python2.7 - вероятно, легче вpython3, но сохранил его здесь, потому что мне потребовалось много проб и ошибок, чтобы заставить subprocess32 работать с phantomjs.
Также - я не поделился файлом thumnails.js
, но это javascript, который анализирует входные данные командной строкив phantomjs для столько URL-адресов, сколько вы хотите, и создает имена файлов, используя эти параметры.