javascript - Puppeteer:从使用延迟加载的页面中抓取整个 html

标签 javascript node.js web-scraping puppeteer

我试图抓取使用延迟加载的网页上的整个 html。我尝试过的是一直滚动到底部,然后使用 page.content()。我还尝试在滚动到底部后滚动回页面顶部,然后使用 page.content()。两种方法都会抓取表格的一些行,但不是全部,这是我的主要目标。我相信该网页使用了react.js 的延迟加载。

const puppeteer = require('puppeteer');
const url = 'https://www.torontopearson.com/en/departures';
const fs = require('fs');

puppeteer.launch().then(async browser => {
    const page = await browser.newPage();
    await page.goto(url);
    await page.waitFor(300);

    //scroll to bottom
    await autoScroll(page);
    await page.waitFor(2500);

    //scroll to top of page
    await page.evaluate(() => window.scrollTo(0, 50));

    let html = await page.content();

    await fs.writeFile('scrape.html', html, function(err){
        if (err) throw err;
        console.log("Successfully Written to File.");
    });
    await browser.close();
});

//method used to scroll to bottom, referenced from user visualxcode on https://github.com/GoogleChrome/puppeteer/issues/305
async function autoScroll(page){ 
    await page.evaluate(async () => {
        await new Promise((resolve, reject) => {
            var totalHeight = 0;
            var distance = 300;
            var timer = setInterval(() => {
                var scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if(totalHeight >= scrollHeight){
                    clearInterval(timer);
                    resolve();
                }
            }, 100);
        });
    });
}

最佳答案

我在这方面不太擅长,但经过长时间的搜索,我发现一种解决方案可以满足我的要求,给出良好的结果。这是我用来处理延迟加载场景的代码。

const bodyHandle = await page.$('body');
const { height } = await bodyHandle.boundingBox();
await bodyHandle.dispose();
console.log('Handling viewport...')
const viewportHeight = page.viewport().height;
let viewportIncr = 0;
while (viewportIncr + viewportHeight < height) {
await page.evaluate(_viewportHeight => {
window.scrollBy(0, _viewportHeight);
}, viewportHeight);
await wait(30);
viewportIncr = viewportIncr + viewportHeight;
}
console.log('Handling Scroll operations')
await page.evaluate(_ => {
window.scrollTo(0, 0);
});
await wait(100);  
await page.screenshot({path: 'GoogleHome.jpg', fullPage: true});

由此我什至可以截取长截图。希望这会对您有所帮助。

关于javascript - Puppeteer:从使用延迟加载的页面中抓取整个 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56052630/

相关文章:

javascript - 使用 Node Express js 下载文件

node.js - AD 授权组更改未反射(reflect)

python - 使用 scrapy 抓取许多页面

node.js - 使用 Node Js 和 Cheerio 解析损坏的 HTML 代码

javascript - React 函数式组件与经典组件

javascript - clone() 的问题 - 事件

javascript - 为什么 Vue.js 模板无法与其脚本交互?

javascript - jQuery js 文件在 css 样式表之后链接时破坏 css 代码

javascript - 无法获取哈希密码以使用 bcrypt 保存

python - 将 Selenium 与 Scrapy 集成