如何在Puppeteer节点js中使用setinterval

时间:2019-05-14 20:34:03

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

我想停止我的脚本,等到最后再返回数组。如果没有操纵up节点js中的return元素,则不应继续前进。它不等待清除间隔并向前移动,因此我无法确定如何等待此处的数组结果。

我得到的结果不确定。我想要一个数组。

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

2 个答案:

答案 0 :(得分:2)

问题是setInterval()不能按预期工作。具体来说,它不会返回您可能Promise的{​​{1}}。它会同步创建间隔,然后返回传递给await的整个函数。

您需要做的是自己创建一个page.evaluate(),并在准备好Promise之后告诉resolve

array

请注意,上面的示例不处理错误。如果您的//... 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将它们传递给外部作用域。

希望这会有所帮助。

答案 1 :(得分:0)

setTimeout,promise和递归函数可能会有所帮助。

// a normal delay function, you can call this with await
const delay = d => new Promise(r => setTimeout(r, d))

const data = [];

async function timer(i = 0) {
  // Optionally set to wait 1000 ms and then continue
  await delay(1000)

  // click element, grab data etc.
  console.log(`Clicking element ${i}`);
  data.push(i);

  // check for condition fulfillment, you can basically put any limit here
  if (i >= 10) return data;

  // return another promise recursively here
  return timer(i + 1)
}

timer().then(console.log)

运行代码片段以查看实际效果。它应该递归地显示控制台,直到达到某个极限为止。

它的工作方式是,如果条件尚未满足,它将返回另一个promise。您可以无限调用它并清除超时(也就是返回一个数据而不是另一个计时器承诺)。