使用 node.js 和 PhantomJS 提取动态内容

Extracting dynamic content with node.js and PhantomJS

本文关键字:动态 提取 PhantomJS node js 使用      更新时间:2023-09-26

我想用nodejs和phantomjs控制台.log网页的内容。这是我的代码:

var phantom = require('phantom');
phantom.create(function(ph) {
    return ph.createPage(function(page) {
        return page.open("http://zehinz.com/test.html", function(status) {
            if (status === 'success') {
               //console.log the content of page with Javascript executed ???
            } else {
                console.log('some error');
                ph.exit();
            }
        });
    });
});

如何输出网页的动态呈现内容?

在普通的PhantomJS中,人们会使用page.content,但由于您使用的是桥接器,因此必须在后台从PhantomJS进程中显式获取content属性。您可以使用 page.get .

在您的情况下,这是

page.get('content', function(content){
    console.log("Content", content);
    ph.exit();
});