62

Webページを要求し、JavaScriptがレンダリングされるのを待って(JavaScriptがDOMを変更する)、ページのHTMLを取得する例を探しています。

これは、PhantomJSの明らかなユースケースを備えた単純な例である必要があります。適切な例が見つかりません。ドキュメントはすべてコマンドラインの使用に関するもののようです。

4

6 に答える 6

45

あなたのコメントから、2つのオプションがあると思います

  1. phantomjs ノード モジュールを見つけてみてください - https://github.com/amir20/phantomjs-node
  2. ノード内の子プロセスとしてphantomjsを実行 - http://nodejs.org/api/child_process.html

編集:

子プロセスは、ノードと対話する方法として phantomjs によって提案されているようです。FAQ を参照してください - http://code.google.com/p/phantomjs/wiki/FAQ

編集:

ページの HTML マークアップを取得するための Phantomjs スクリプトの例:

var page = require('webpage').create();  
page.open('http://www.google.com', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var p = page.evaluate(function () {
            return document.getElementsByTagName('html')[0].innerHTML
        });
        console.log(p);
    }
    phantom.exit();
});
于 2012-04-02T14:20:58.443 に答える
8

のv2phantomjs-nodeでは、処理後に HTML を簡単に印刷できます。

var phantom = require('phantom');

phantom.create().then(function(ph) {
  ph.createPage().then(function(page) {
    page.open('https://stackoverflow.com/').then(function(status) {
      console.log(status);
      page.property('content').then(function(content) {
        console.log(content);
        page.close();
        ph.exit();
      });
    });
  });
});

これにより、ブラウザでレンダリングされた出力が表示されます。

編集 2019:

使用できますasync/await

const phantom = require('phantom');

(async function() {
  const instance = await phantom.create();
  const page = await instance.createPage();
  await page.on('onResourceRequested', function(requestData) {
    console.info('Requesting', requestData.url);
  });

  const status = await page.open('https://stackoverflow.com/');
  const content = await page.property('content');
  console.log(content);

  await instance.exit();
})();

または、テストしたいだけの場合は、使用できますnpx

npx phantom@latest https://stackoverflow.com/
于 2016-03-15T18:26:58.740 に答える
4

Declan が言及した DOM を照会する page.evaluate() メソッドを含む、過去に 2 つの異なる方法を使用しました。Web ページから情報を渡すもう 1 つの方法は、そこから console.log() に情報を吐き出し、phantomjs スクリプトで次のように使用することです。

page.onConsoleMessage = function (msg, line, source) {
  console.log('console [' +source +':' +line +']> ' +msg);
}

onConsoleMessage で変数 msg をトラップして、カプセル化されたデータを検索することもできます。出力をどのように使用するかによって異なります。

次に、Nodejs スクリプトで、Phantomjs スクリプトの出力をスキャンする必要があります。

var yourfunc = function(...params...) {
  var phantom = spawn('phantomjs', [...args]);
  phantom.stdout.setEncoding('utf8');
  phantom.stdout.on('data', function(data) {
    //parse or echo data
    var str_phantom_output = data.toString();
    // The above will get triggered one or more times, so you'll need to
    // add code to parse for whatever info you're expecting from the browser
  });
  phantom.stderr.on('data', function(data) {
    // do something with error data
  });
  phantom.on('exit', function(code) {
    if (code !== 0) {
      // console.log('phantomjs exited with code ' +code);
    } else {
      // clean exit: do something else such as a passed-in callback
    }
  });
}

それがいくつか役立つことを願っています。

于 2012-05-31T20:21:08.190 に答える
3

なぜこれを使わないのですか?

var page = require('webpage').create();
page.open("http://example.com", function (status)
{
    if (status !== 'success') 
    {
        console.log('FAIL to load the address');            
    } 
    else 
    {
        console.log('Success in fetching the page');
        console.log(page.content);
    }
    phantom.exit();
});
于 2013-12-18T16:07:38.217 に答える
1

これは、ページを.pngとして保存する実行中のnode、express、およびphantomjsを使用する古いバージョンです。html を取得するためにかなり迅速に微調整できます。

https://github.com/wehrhaus/sitescrape.git

于 2014-04-26T03:18:32.117 に答える