木偶无法使用评估获取页面的innerHTML

时间:2018-08-28 15:17:13

标签: javascript jquery node.js puppeteer

我正在尝试从网站获取HTML,但是伪娘无法获取HTML。相同的代码可用于我正在下载的其他多个站点。

是什么让该网站与众不同并不允许评估功能获得内容?

即使jQuery也无法访问此网站上的元素。下载的文件似乎确实具有iframe,但是我不确定是否是问题所在。

const puppeteer = require('puppeteer');
const fse = require('fs-extra');
const fs = require('fs');
const nodepath = require('path')
var url = 'http://example.com';

var oddsOutputPath = nodepath.join(__dirname, '..', 'public', 'outputdir');

console.log(oddsOutputPath);
fse.ensureDir(oddsOutputPath).then(() => { console.log('success!') }).catch(err => { console.error(err) });

function delay(timeout) {
  return new Promise((resolve) => {
    setTimeout(resolve, timeout);
  });
}

(async () => {
  const browser = await puppeteer.launch({
    networkIdleTimeout: 0,
    timeout: 360000,
    headless: false,
    ignoreHTTPSErrors: true,
    args: [
      '--no-sandbox'
    ]
  });

  const page = await browser.newPage();
  await page.setRequestInterception(true);
  page.on('request', request => {
    if (['image', /* 'stylesheet', */ 'font'].indexOf(request.resourceType()) !== -1) {
      request.abort();
    } else {
      request.continue();
    }
  });

  try {
    await page.setViewport({ width: 1366, height: 653 });
    await page.goto(url, { waitUntil: 'networkidle2', timeout: 360000 });
    await delay(45000);

    outputPath = oddsOutputPath + "/init.html";
    let html = await page.evaluate(() => document.body.innerHTML);
    fs.writeFileSync(outputPath, html);


    console.log('script has ended');
    await delay(5000);


  } catch (e) {
    var error = "'" + e + "'";
    console.log(error)
    process.exit()
  }
  finally {
    await browser.close();
  }


})();

1 个答案:

答案 0 :(得分:1)

您要访问的页面将其大部分内容加载到iframe中。

我建议直接访问iframe以抓取其内容:

git apply stash2@{N}