【发布时间】:2020-10-11 01:04:15
【问题描述】:
我试图从超市网站的所有类别中获取所有产品名称和价格,我发现的所有教程都只针对一个 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。我怎样才能做到这一点?
【问题讨论】:
标签: html node.js web-scraping puppeteer