鍍金池/ 問(wèn)答/數(shù)據(jù)分析&挖掘  HTML/ nodeJS的phantomJS網(wǎng)站截圖如何等待網(wǎng)頁(yè)腳本執(zhí)行完?(比如js執(zhí)行a

nodeJS的phantomJS網(wǎng)站截圖如何等待網(wǎng)頁(yè)腳本執(zhí)行完?(比如js執(zhí)行ajax獲取的數(shù)據(jù))

問(wèn)題描述

現(xiàn)在我要抓取一個(gè)網(wǎng)站的截圖。

但是我發(fā)現(xiàn)截圖出來(lái)的是未執(zhí)行頁(yè)面腳本JS的圖,我看了很多文檔和網(wǎng)友解答,無(wú)奈自己的搜索能力有點(diǎn)差,一直找不到對(duì)應(yīng)的問(wèn)題解決方案。

比如這個(gè)頁(yè)面有大概幾個(gè)XHR類(lèi)型獲取的數(shù)據(jù)形成的列表,我該如何在頁(yè)面完全加載和執(zhí)行JS后再去截圖?

下面是我的代碼

const phantom = require('phantom');

let count = 0;

(async function () {
  const instance = await phantom.create();
  const page = await instance.createPage();
  const status = await page.open('http://wenshu.court.gov.cn/list/list/?sorttype=1&number=TMSVETRT&guid=31e005b5-868f-ae69cf1d-c65e96bcf639&conditions=searchWord+%E5%BD%93%E4%BA%8B%E4%BA%BA+++%E5%BD%93%E4%BA%8B%E4%BA%BA:%E5%BA%84%E6%9C%9D%E6%99%96');
  await page.evaluate(function() { // 是在這里執(zhí)行嗎?有疑問(wèn)
    return document.body.innerHTML;
  }).then(function(html){
    console.log(html);
  });
  if (status == 'success') {
     await page.render('./lalal.png');
  }
  await instance.exit();
})();


回答
編輯回答
悶騷型

別用phantomjs了,現(xiàn)在chrome都支持headless了,p的作者自己都說(shuō)不建議使用了。
puppeteer和selenium有很多選擇的。

2018年6月16日 07:14
編輯回答
離魂曲

puppeteer.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('http://wenshu.court.gov.cn/list/list/?sorttype=1&number=TMSVETRT&guid=31e005b5-868f-ae69cf1d-c65e96bcf639&conditions=searchWord+%E5%BD%93%E4%BA%8B%E4%BA%BA+++%E5%BD%93%E4%BA%8B%E4%BA%BA:%E5%BA%84%E6%9C%9D%E6%99%96');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();
2018年1月6日 02:56