0

I'm trying pick some data from followers page, but always return a empty array. That's my code:

    const puppeteer = require('puppeteer');


(async () => {
  const browser = await puppeteer.launch({headless:false});
  const page = await browser.newPage();

  await page.goto('https://www.instagram.com/accounts/login/', {
    waitUntil: 'networkidle0',
  });

  // Wait for log in form

  await Promise.all([
    page.waitForSelector('[name="username"]'),
    page.waitForSelector('[name="password"]'),
    page.waitForSelector('[type="submit"]'),
  ]);

  // Enter username and password

  await page.type('[name="username"]', 'yourAccount');
  await page.type('[name="password"]', 'yourPassword');

  // Submit log in credentials and wait for navigation

  await Promise.all([
    page.click('[type="submit"]'),
    page.waitForNavigation({
      waitUntil: 'networkidle0',
    }),
  ]);

  await page.goto('https://www.instagram.com/publicProfile /followers/', {waitUntil: 'networkidle0'});

  const teste = await page.evaluate(() => {
    const followers = document.querySelectorAll("._aaco span");
    let followersArray = []
    followers.forEach((item) =>{
      followersArray.push(item.innerText)
    })
    return followersArray
  })

  console.log(teste)

  await browser.close();
})();

publicProfile in the URL is a profile that I choose, but for privacy reasons e alterate for publicProfile.

UPDATE: The problem has resolved, as Örvar said the problem was that I wasn't logged, soo I search for help here and I found this (Puppeteer Login to Instagram) question that solved my question.

  • What do you get if you `console.log` `followers`? I suspect that selecting on `.__aaco` is unstable. That class name looks like it may be generated when the page loads, but that's just a hunch – Souperman Aug 01 '22 at 06:00
  • I've tried this tag many times in many browsers and still the same, and followers is a nodeList, soo console only print {} – Guilherme Santiago Aug 01 '22 at 06:10
  • I think if there were matches it would at least print the number of hits. It sounds to me like your selector string is not matching what you think it is. Do you see results if you run just that line in the browser console for yourself? – Souperman Aug 01 '22 at 06:15
  • Yes, generate one array – Guilherme Santiago Aug 01 '22 at 06:17
  • In that case the only other thing I can see is you have a space in the URL you are `goto`ing. That would likely take you to a 404 page rather than the page you're expecting? – Souperman Aug 01 '22 at 06:24

1 Answers1

0

When you use a tool like Puppeteer to get content from a site where you need to login, you also need to login using Puppeteer, so the site that you are logging into will generate a user cookie.

  1. Log into Instagram using Puppeteer with user credentials

  2. When Puppeteer has logged in with user credentials, you can run the code you have posted above.

IceCode
  • 1,466
  • 13
  • 22
  • I've added some code and still don't working, please check my question again – Guilherme Santiago Aug 01 '22 at 05:57
  • When I try with the following: ``document.querySelectorAll("._aaco span");`` I dont get any results, but if I try ``document.querySelectorAll("._aaco");`` I get results. – IceCode Aug 01 '22 at 06:41
  • You can try ``document.querySelectorAll("._aaco");`` in the console in the Chrome developer tool to see if it gives you any results. https://developer.chrome.com/docs/devtools/console/javascript/ That way you can quickly check if the selecter you have chosen actually does return results on any webpage that you are on. – IceCode Aug 01 '22 at 06:44