javascript 如何将 PhantomJS 作为服务器运行并远程调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30713657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:35:07  来源:igfitidea点击:

How to run PhantomJS as a server and call it remotely?

javascriptnode.jsseophantomjs

提问by Michael

This is probably a very basic question. I would like to run a headless browser PhantomJSas a server but not as a command line tool.

这可能是一个非常基本的问题。我想将无头浏览器PhantomJS作为服务器运行,而不是作为命令行工具运行。

Once it is running I would like to call it remotely over HTTP. The only thing I need is to send a URL and get back the HTML output. I need it to generate HTML for an AJAX application to make it searchable.

一旦它运行,我想通过 HTTP 远程调用它。我唯一需要的是发送一个 URL 并取回 HTML 输出。我需要它为 AJAX 应用程序生成 HTML 以使其可搜索。

Is it possible ?

是否可以 ?

回答by Artjom B.

You can run PhantomJS perfectly fine as a webserver, because it has the Web Server Module. The examples folder contains for example a server.js example. This runs standalone without any dependencies (without node).

您可以完美地将 PhantomJS 作为网络服务器运行,因为它具有Web 服务器模块。例如,示例文件夹包含一个 server.js 示例。它独立运行,没有任何依赖项(没有节点)。

var page = require('webpage').create(),
    server = require('webserver').create();

var service = server.listen(port, function (request, response) {
    console.log('Request received at ' + new Date());
    // TODO: parse `request` and determine where to go
    page.open(someUrl, function (status) {
        if (status !== 'success') {
            console.log('Unable to post!');
        } else {
            response.statusCode = 200;
            response.headers = {
                'Cache': 'no-cache',
                'Content-Type': 'text/plain;charset=utf-8'
            };
            // TODO: do something on the page and generate `result`
            response.write(result);
            response.close();
        }
    });
});

If you want to run PhantomJS through node.js then this is also easily doable using the phantomjs-nodewhich is a PhantomJS bridge for node.

如果您想通过 node.js 运行 PhantomJS,那么使用phantomjs-node也很容易实现,它是节点的 PhantomJS 桥接器。

var http = require('http');
var phantom = require('phantom');

phantom.create(function (ph) {
  ph.createPage(function (page) {
    http.createServer(function (req, res) {
      // TODO: parse `request` and determine where to go
      page.open(someURL, function (status) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        // TODO: do something on the page and generate `result`
        res.end(result);
      });
    }).listen(8080);
  });
});

Notes

笔记

You can freely use this as is as long you don't have multiple requests at the same time. If you do, then you either need to synchronize the requests (because there is only one pageobject) or you need to create a new pageobject on every request and close()it again when you're done.

只要您没有同时有多个请求,您就可以按原样自由使用它。如果这样做,那么您要么需要同步请求(因为只有一个page对象),要么需要page在每个请求上创建一个新对象,并close()在完成后再次创建。

回答by Jay Bhagat

The easiest way is to make a python script or something simple to start the server and use python websockets to communicate with it, using a web form of sorts to query for a website and get the page source. Any automation can be done via cron jobs, or if you are on Windows, you may use the Tasks feature to autostart the python script.

最简单的方法是制作一个python脚本或一些简单的东西来启动服务器并使用python websockets与之通信,使用各种网络形式来查询网站并获取页面源。任何自动化都可以通过 cron 作业完成,或者如果您使用的是 Windows,您可以使用任务功能来自动启动 python 脚本。