JSFiddle - React, Tailwind, and code Playground

by Hassen Charef

HTML

<script async src="https://unpkg.com/[email protected]/index.js"></script>

JavaScript

let fun = async () => {
  console.log("test")
  let browser, page;
  let url = "https://www.facebook.com/marketplace/nyc/search/?query=cars";
  browser = await puppeteer.launch({
    headless: false
  });
  console.log("browser")
  page = await browser.newPage();
  await page.goto(url, {
    waitUntil: "networkidle2"
  });
  console.log("scraping...");
  let data = await page.evaluate((dat) => {
    console.log(dat)
    let cars = [];
    if (
      document.querySelectorAll(
        'div[class="fome6x0j tkqzz1yd aodizinl fjf4s8hc f7vcsfb0"]'
      )
    ) {
      document.querySelectorAll(
          'div[class="fome6x0j tkqzz1yd aodizinl fjf4s8hc f7vcsfb0"]'
        )
        .forEach((element) => {

          let addLists = element.querySelectorAll(
            'span[class="a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7"]'
          );

          for (let i = 0; i < addLists.length; i++) {
            const car = {
              price: prices[i].innerText,
              listing: listings[i].innerText,
              addList: addLists[i].innerText,
              sentence1: '',
              sentence2: ''
            };

            cars.push(car);
          }
        });
    }

    return cars;
  });

  console.log(data);
};

fun();