Как сканировать веб-сайт javascript (vuejs, reactjs) с помощью nodejs - PullRequest
0 голосов
/ 02 февраля 2019

Я собирался сканировать веб-сайт vue js, когда я пытаюсь сканировать, что он не загружает контент в cheerio ... что я получаю, пустую веб-страницу.мой код выглядит следующим образом

getSiteContentAsJs = (url) => {
  return new Promise((resolve, reject) => {
    let j = request.jar();
    request.get({url: url, jar: j}, function(err, response, body) {
        if(err)
          return resolve({body: null, jar: j, error: err});
        return resolve({body: body, jar: j, error: null});
    });
  })
}

Я получил свое содержимое следующим образом

const { body, jar, error} = await getSiteContentAsJs(url);
//I passed body to cheerio to get the js object out of the web content
const $ = cheerio.load(body);

, но ничего не отображается.но пустая веб-страница.в нем нет содержимого.

1 Ответ

0 голосов
/ 02 февраля 2019

Я обнаружил, что Cheerio не запускает JavaScript.так как этот веб-сайт основан на vue front end, мне нужен виртуальный браузер, который на самом деле запускает js и выводит мне вывод

, поэтому вместо использования request я использовал фантом для рендеринга js веб-страниц

const phantom = require('phantom');
const cheerio = require('cheerio');

loadJsSite = async (url) => {
  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(url);
  const content = await page.property('content');
  // console.log(content);
  // let $ = cheerio.load(content);

  await instance.exit();

  return {$: cheerio.load(content), content: content};
} 

теперь я могу получить обработанную страницу, как показано ниже

const {$, content} = await loadJsSite(url);
// I can query like this
// get the body
$('body').html();
...