html - 如何遍历超市网站并获取产品名称和价格?

标签 html node.js web-scraping puppeteer

我试图从超市网站获取所有类别的所有产品名称和价格,我发现的所有教程都只是针对一个 const url,我需要遍历所有这些。到目前为止我已经得到了这个

const puppeteer = require('puppeteer');

async function scrapeProduct(url) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);

    const [el2] = await page.$x('//*[@id="product-nonfood-page"]/main/div/div/div[1]/div[1]/div/div[2]/h1/div');
    const text2 = await el2.getProperty('textContent');
    const name = await text2.jsonValue();

    const [el] = await page.$x('//*[@id="product-nonfood-page"]/main/div/div/div[1]/div[1]/div/div[2]/div[2]/div[1]/div[2]/p[1]/em[2]/strong/text()');
    const text = await el.getProperty('textContent');
    const price = await text.jsonValue();

    console.log({name,price});

    await browser.close();
}

scrapeProduct('https://www.jumbo.com.ar/gaseosa-sprite-sin-azucar-lima-limon-1-25-lt/p'); 

仅适用于一个。我使用的是nodejs 和puppeteer。我怎样才能实现这个目标?

最佳答案

您可以尝试 for...of 循环,使用单个浏览器实例和单个页面,以便抓取工具不会使服务器过载:

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    const urls = [
      'https://www.jumbo.com.ar/gaseosa-sprite-sin-azucar-lima-limon-1-25-lt/p',
      // ...
    ];

    for (const url of urls) {
      await page.goto(url);

      const [el2] = await page.$x('//*[@id="product-nonfood-page"]/main/div/div/div[1]/div[1]/div/div[2]/h1/div');
      const text2 = await el2.getProperty('textContent');
      const name = await text2.jsonValue();

      const [el] = await page.$x('//*[@id="product-nonfood-page"]/main/div/div/div[1]/div[1]/div/div[2]/div[2]/div[1]/div[2]/p[1]/em[2]/strong/text()');
      const text = await el.getProperty('textContent');
      const price = await text.jsonValue();

      console.log({name,price});
    }

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();

关于html - 如何遍历超市网站并获取产品名称和价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64295559/

相关文章:

javascript - 为什么在用 document.write() 编写时拆分 <script> 标签?

html - 在手机上观看时,嵌入式 Youtube 视频会 float 到右侧

javascript - 命令参数未运行我的其余代码

node.js - 与 Google Hangouts 和 IBM Watson 集成

javascript - 使用 alethes 进行分页 :pages using a subscription

java - 如何让 jsoup 等待完整页面(跳过进度页面)加载?

ruby - 我如何用 Mechanize (使用 cookie)抓取谷歌阅读器

python - 如何从 XPath 中的类属性获取标题(Python/scrapy)

javascript - 如何在基于 JSON 结构的复选框中显示内部子 bool 值?

javascript - 如何在javascript中将日期时间从用户时区转换为EST