phantomjs rasterize有url参数问题

时间:2014-09-09 20:57:18

标签: phantomjs url-parameters

我在使用标准命令行工具并使用PhantomJS和修改后的rasterize.js代码的Windows计算机上。我遇到的问题是当我通过网址http://time.com/3274245/e-cigarettes-debate/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+time/topstories+(TIME:+Top+Stories)时。我已经重定向了StandardOutputStandardError,这是我用上面的网址得到的。

StandardOutput

Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]
  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"

StandardErrror

'utm_source' is not recognized as an internal or external command,
operable program or batch file.
'utm_medium' is not recognized as an internal or external command,
operable program or batch file.
'utm_campaign' is not recognized as an internal or external command,
operable program or batch file.

所以问题是,有没有办法解决网址中参数的问题?

如果有任何遗失的信息或有任何需要澄清的信息,请告诉我。

我将在下方添加修改后的rasterize.js

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

page.settings.userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:31.0) Gecko/20100101 Firefox/31.0';
if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    page.viewportSize = { width: 1200, height: 1200 };
    if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
        size = system.args[3].split('*');
        page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
                                           : { format: system.args[3], orientation: 'portrait', margin: '1cm' };
    }
    if (system.args.length > 4) {
        page.zoomFactor = system.args[4];
    }
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 10000);
        }
    });
}

1 个答案:

答案 0 :(得分:4)

在Windows cmd中,您需要在可能包含特殊字符的参数周围使用引号。当事情发生时,=是一个很好的候选人。

phantomjs rasterize.js "http://time.com/..." time.png

对于此页面,您可能需要禁用Web安全性:

phantomjs --web-security=false rasterize.js "http://time.com/..." time.png