Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How to get opening hours on nodeJS

$
0
0

I am trying to get restaurant listings for a data analyst project. This is trying to search for restaurants in Seattle. I'm thinking why it couldn't get opening hours is due to selectors. The current output of this includes name, address, phone number and plus codes.

import xlsx from "xlsx";(async () => {  const browser = await puppeteer.launch({ headless: "new" });  const page = await browser.newPage();  // Set the navigation timeout value  page.setDefaultNavigationTimeout(60000);  await page.goto("https://www.google.com/maps/@48.1403077,17.1036957,70m/data=!3m1!1e3?entry=ttu");  // Set screen size  await page.setViewport({ width: 1080, height: 1024 });  // Type into search box  await page.type("#searchboxinput", "restaurant+seattle");  // Wait and click on first result  const searchResultSelector = ".mL3xi";  await page.waitForSelector(searchResultSelector);  await page.click(searchResultSelector);  await page.waitForSelector(".hfpxzc");  const places = [];  for (let i = 0; i < 1; i++) {    console.log(i);    const element = await page.evaluateHandle(      (index) => document.querySelectorAll(".hfpxzc")[index],      i    );    if (element) {      try {        await element.click();        await page.waitForNavigation();        await page.waitForSelector(".CsEnBe");        await page.waitForTimeout(1500);        const placeName = await page.evaluate(          () => document.querySelectorAll(".DUwDvf")[0].innerText        );        const existingPlace = places.find((place) => place.name === placeName);        if (!existingPlace) {          const items = await page.evaluate(            () => document.querySelectorAll(".CsEnBe").length          );          const info = {};          info["Name"] = placeName;          for (let i = 0; i < items; i++) {            const innerText = await page.evaluate(              (index) => document.querySelectorAll(".CsEnBe")[index].innerText,              i            );            const tooltip = await page.evaluate(              (index) =>                document.querySelectorAll(".CsEnBe")[index].dataset.tooltip,              i            );            // Wait for 1 second before proceeding iteration            await page.waitForTimeout(1000);            if (tooltip == "Copy address") {              info["Address"] = innerText;            } else if (tooltip == "Open website") {              info["Website"] = `https://www.${innerText}`;            } else if (tooltip == "Copy phone number") {              info["Phone Number"] = innerText;            } else if (              tooltip == "Open menu link" ||              tooltip == "Place an order" ||              tooltip == "Open reservation link" ||              tooltip == undefined            ) {            } else {              info[tooltip] = innerText;            }          }          places.push(info);        }      } catch (error) {        console.log(error);      }      await page.evaluate(() => {        const scrollElement = document.querySelectorAll(".ecceSd")[1];        scrollElement.scrollBy(0, 300);      });    } else {      break;    }  }  console.log(places);  const wb = xlsx.utils.book_new();  const ws = xlsx.utils.json_to_sheet(places);  xlsx.utils.book_append_sheet(wb, ws, "Data");  xlsx.writeFile(wb, "Places.xlsx");  await browser.close();})();type here

I am getting quite confused with the selectors, tried manually and AI, but they don't seem to work, any idea on how I can get opening hours from this.your text


Viewing all articles
Browse latest Browse all 12111

Trending Articles