node.js - 如何在 Puppeteer Node js 中使用 setInterval

标签 node.js web-scraping async-await instagram puppeteer

我想停止我的脚本并等到结束然后返回数组。如果 puppeteer Node js 中没有返回元素,它不应该向前移动。它不等待清除间隔并向前移动,导致我未定义,这里如何等待数组的结果。

我得到的结果未定义。我想要一个数组。

const puppeteer = require("puppeteer");
var page;
var browser;
async function getuser_data(callback) {
    browser = await puppeteer.launch({
        headless: false,
        args: ["--no-sandbox", "--disable-setuid-sandbox"]
    });
    page = await browser.newPage();
    await page.setViewport({
        width: 1068,
        height: 611
    });
    await page.goto(
        "https://www.instagram.com/accounts/login/?source=auth_switcher"
    );
    await page.waitForSelector('input[name="username"]');
    await page.type('input[name="username"]', "yourusername");
    await page.type('input[name="password"]', "yourpassword");
    await page.click("._0mzm-.sqdOP.L3NKy");

    await page.waitFor(3000);
    var y = "https://www.instagram.com/xyz/";
    await page.goto(y);
    await page.waitFor(2000);

    var c = await page.evaluate(async () => {
        await document
            .querySelector(
                "#react-root > section > main > div > header > section > ul > li:nth-child(2) > a"
            )
            .click();
        var i = 0;
        var timer = await setInterval(async () => {
            i = i + 1;
            console.log(i);
            await document.querySelector(".isgrP").scrollBy(0, window.innerHeight);
            var ele = await document.querySelectorAll(".FPmhX.notranslate._0imsa ")
                .length;
            console.log("Now length is :" + ele);
            console.log("Timer :" + i);

            if (ele > 10 && i > 20) {
                console.log("Break");
                clearInterval(timer);
                console.log("after break");
                var array = [];
                for (var count = 1; count < ele; count++) {
                    try {
                        var onlyuname = await document.querySelector(
                            `body > div.RnEpo.Yx5HN > div > div.isgrP > ul > div > li:nth-child(${count}) > div > div.t2ksc > div.enpQJ > div.d7ByH > a`
                        ).innerText;
                        console.log(onlyuname);
                        var obj = {
                            username: onlyuname
                        };
                        console.log(obj);
                        await array.push(obj);
                    } catch (error) {
                        console.log("Not found");
                    }
                }
                console.log(JSON.stringify(array));
                return array;   //Should Wait Till return , it should not move forward
            }
        }, 800);
    });
    console.log(c)  //IT should return me array, Instead of undefined
    callback(c)
}

getuser_data(users => {
    console.log(users)
    let treeusernamefile = JSON.stringify(users);
    fs.writeFileSync('tablebay.json', treeusernamefile);
})

最佳答案

问题是 setInterval() 无法按您的预期工作。具体来说,它不会返回您可以awaitPromise。它同步创建间隔,然后传递给 page.evaluate() 的整个函数返回。

您需要做的是自己创建一个Promise,并在准备好数组后告诉它resolve

//...

return new Promise((resolve, reject) => {
    var timer = setInterval(async () => {
            i = i + 1;
            console.log(i);
            await document.querySelector(".isgrP").scrollBy(0, window.innerHeight);
            var ele = await document.querySelectorAll(".FPmhX.notranslate._0imsa ")
                .length;
            console.log("Now length is :" + ele);
            console.log("Timer :" + i);

            if (ele > 10 && i > 20) {
                console.log("Break");
                clearInterval(timer);
                console.log("after break");
                var array = [];
                for (var count = 1; count < ele; count++) {
                    try {
                        var onlyuname = await document.querySelector(
                            `body > div.RnEpo.Yx5HN > div > div.isgrP > ul > div > li:nth-child(${count}) > div > div.t2ksc > div.enpQJ > div.d7ByH > a`
                        ).innerText;
                        console.log(onlyuname);
                        var obj = {
                            username: onlyuname
                        };
                        console.log(obj);
                        await array.push(obj);
                    } catch (error) {
                        console.log("Not found");
                    }
                }
                console.log(JSON.stringify(array));
                resolve(array);   // <-----------------
            }
        }, 800);
})

//...

请注意,上面的示例不处理错误。如果 setInterval 中的任何函数抛出异常,您需要捕获这些错误并使用 reject 将它们传递到外部作用域。

希望这有帮助。

关于node.js - 如何在 Puppeteer Node js 中使用 setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56138217/

相关文章:

mysql - 如何基于Node.js为书店制作正确的BD

javascript - 抓取javascript网站

python - 网页抓取器返回元素列表

c# - 如何使用 AngleSharp & LINQ 从网站中提取数据?

c# - C# 中的 Fire and Forget 多个方法

javascript - 如何在 vuejs 中发出响应之前等待 axios 响应

node.js - 使用node js删除mongodb中的特定数据

node.js - ubuntu VM 上的 node-expat 安装错误

node.js - 如何编写查询,某些对象的条件不能位于对象数组中,而其他一些对象之一必须位于对象数组中?

使用异步等待的 Angular http get