Extract all ids with puppter from flashScore

Viewed 56

I need to extract all divs from https://www.flashscore.es/. With following code I can print all elements that I need:

const puppeteer = require('puppeteer');
const fs = require('fs');
const list = [];

(async () => {
  const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1920, height: 1080 }, args: ['--start-maximized'] });
  const page = await browser.newPage();
  let element, formElement, tabs;
  await page.goto(`https://www.flashscore.es/baloncesto/`);
  await delay(4000);
  element = await page.$x(`//div[starts-with(@id,'g_3_')]`);

  console.log(element);
})();

function delay(time) {
  return new Promise(function(resolve) { 
    setTimeout(resolve, time)
  });
}

The HTML of the elements looks like this:

<div id="g_3_Qyk44tlB" title="¡Haga click para detalles del partido!" elementtiming="SpeedCurveFRP" class="event__match event__match--live event__match--twoLine">

How to get ids of the needed elements?

1 Answers

You are already getting the required elements, now let's go over all of them and extract the "id" property:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: false, defaultViewport: { width: 1920, height: 1080 }, args: ['--start-maximized'] });
  const page = await browser.newPage();

  await page.goto(`https://www.flashscore.es/baloncesto/`);
  
  // More reliable than wait for an arbitrary N of seconds
  await page.waitForSelector('[id^="g_3_"]');

  const ids = [];
  const elements = await page.$x(`//div[starts-with(@id,'g_3_')]`);
  for(const elementHandle of elements) {
    const jsHandle = await elementHandle.getProperty("id")
    ids.push(await jsHandle.jsonValue());
  }
  console.log(ids);
  await page.close();
  await browser.close();
})();

Result:
A screenshot of found ids in the console

P.S. There's a built-in function for waiting in puppeteer:

await page.waitForTimeout(4000);
Related