繁体   English   中英

python子进程不会运行phantomjs,但可以在linux命令行中使用

[英]python subprocess won't run phantomjs, but works in linux command line

当我在CENTOS 7服务器上运行它时,它可以从bash运行:

[myserver]$ /home/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /home/phantomjs-2.1.1-linux-x86_64/bin/thumbnails.js -3933029 91 q5975 "http://mysite/explore?viz=summary_slider"
Rendered 'http://mysite/explore?viz=summary_slider' at '/home/thumbnails/th-3933029c91q5975.png'

但是,如果我使用子进程在python中执行此操作,则会收到错误消息:

import subprocess
phantomjs_call = u'{0}phantomjs {0}thumbnails.js {1}'.format(phantomjspath, link)
rendered = subprocess.check_output(phantomjs_call.split())

回报

/home/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /home/phantomjs-2.1.1-linux-x86_64/bin/thumbnails.js "http://mysite/explore?viz=summary_checkbox"
Unable to render '"http://mysite/explore?viz=summary_checkbox"'

子流程参数有什么怪异的吗? 还是外壳环境不对?

接下来,我对其进行了调整,并将完整的字符串作为一个参数传入,然后得到OSError:

rendered = subprocess.check_output(phantomjs_call)
# didn't split this into multiple arguments
>>>[Errno 2] no such file or directory"

那个怎么样

import subprocess
phantomjs_call = '{0}phantomjs {0}thumbnails.js {1}'.format(phantomjspath, link)
print(subprocess.check_output(phantomjs_call), shell=True)

要么

import os
phantomjs_call = '{0}phantomjs {0}thumbnails.js {1}'.format(phantomjspath, link)
print(os.system(phantomjs_call))

因此,在尝试许多不同的变化后, subprocess ,这是与工作phantomjssubprocess32

    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,并使用这些参数构造文件名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM