如何在 Puppeteer Node js 中使用 setInterval

Imr*_*lal 2 node.js web-scraping async-await instagram puppeteer

我想停止我的脚本并等到结束然后返回数组。如果 puppeteer 节点 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);
})

Run Code Online (Sandbox Code Playgroud)

Ond*_*ban 5

问题是它setInterval()并不像你期望的那样工作。具体来说,它不会返回 a Promisethat you can await。它同步创建间隔,然后您传递给的整个函数page.evaluate()返回。

你需要做的是创建一个你自己,并在你准备好后Promise告诉它。resolvearray

//...

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);
})

//...
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助。