Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

使用Phantom和node,将网页变成图片,放在函数里无效

在utils文件里有phantom.js和capture.js两个文件phantom.js
我调用/send接口,希望执行phantom.js里面的功能,
但是没有效果,请问是什么原因?

const spawn = require('child_process').spawn;

let Phantom = {
    createImage: ()=> {
        // 捕获标准输出并将其打印到控制台

        return new Promise(function (resolve, reject) {
            const process = spawn('phantomjs',[ __dirname + '/capture.js']);
            // console.log(processor.cwd());

            process.stdout.on('data', function (data) {
                console.log('标准输出:
' + data);
                resolve(data);
            });

// 捕获标准错误输出并将其打印到控制台
            process.stderr.on('data', function (data) {
                console.log('标准错误输出:
' + data);
            });

// 注册子进程关闭事件
            process.on('exit', function (code, signal) {
                console.log('子进程已退出,代码:' + code);
            });
        });
    },
};

module.exports = Phantom;

capture.js

var page=require('webpage').create();//创建一个网页对象;
var system=require('system');
var address,fileName;
// page.viewportSize={width:1024,height:800};//设置窗口的大小为1024*800;
// page.clipRect={top:0,left:0,width:1024,height:800};//截取从{0,0}为起点的1024*800大小的图像;
// //禁止Javascript,允许图片载入;
// // 并将userAgent改为"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0";
// page.settings={
//     javascriptEnabled: false,
//     loadImages: true,
//     userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0'
// };


var page = require('webpage').create();
page.open('https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&word=phantom+open', function() {
    page.render('baidu.png');
    phantom.exit();
});

在另外的routes文件夹里有poster.js

/**
 * 租房信息
 */
const app = require('express').Router();
const db = require('../../modules/database');
const helper = require('../../utils/helper');
const config = require('../../modules/configuration');
const phantom = require('../../modules/utils/phantom');

/**
 * 更新租房信息
 */
app.all('/send', (req, res) => {
    
    let returnResult = {};


    phantom.createImage().then(data=>{
        console.log(data)
    });



});

module.exports=app;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...