如何获取nodeJS上的营业时间

问题描述 投票:0回答:1

我正在尝试获取数据分析项目的餐厅列表。这是试图搜索西雅图的餐馆。我在想为什么它无法获得开放时间是由于选择者的原因。当前的输出包括姓名、地址、电话号码和加号。

import puppeteer from "puppeteer";
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

我对选择器感到非常困惑,尝试了手动和人工智能,但它们似乎不起作用,不知道如何从中获取营业时间。

your text

node.js google-maps web-scraping
1个回答
0
投票

如果您需要的数据量比较小,可以直接使用Google提供的API。

Google 地图搜索最多返回 120 个结果。要抓取更多结果,例如纽约市的所有餐馆,在搜索之前必须将大区域分成小区域。

© www.soinside.com 2019 - 2024. All rights reserved.