代码之家  ›  专栏  ›  技术社区  ›  Marc Maxmeister

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

  •  0
  • Marc Maxmeister  · 技术社区  · 6 年前

    [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"'
    

    子进程args有什么奇怪的地方吗?还是外壳环境不对?

    rendered = subprocess.check_output(phantomjs_call)
    # didn't split this into multiple arguments
    >>>[Errno 2] no such file or directory"
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Artsiom Praneuski    6 年前

    那怎么样

    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))
    
        2
  •  0
  •   Marc Maxmeister    6 年前

    所以,在尝试了很多不同的变化 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,并使用这些参数构造文件名。