我是一个使用Puppeteer进行爬虫的初学者。我成功地爬取了以下网站。下面是从购物商城中提取特定产品名称的代码。
const express = require('express');
const puppeteer = require('puppeteer');
const app = express();
(async () => {
const width = 1600, height = 1040;
const option = { headless: true, slowMo: true, args: [`--window-size=${width},${height}`] };
const browser = await puppeteer.launch(option);
const page = await browser.newPage();
const vp = {width: width, height: height};
await page.setViewport(vp);
const navigationPromise = page.waitForNavigation();
// 네이버 스토어팜
await page.goto('https://shopping.naver.com/home/p/index.nhn');
await navigationPromise;
await page.waitFor(2000);
const textBoxId = 'co_srh_input';
await page.type('.' + textBoxId, '양말', {delay: 100});
await page.keyboard.press('Enter');
await page.waitFor(5000);
await page.waitForSelector('ul.goods_list');
await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'});
const result = await page.evaluate(() => {
const data = [];
$('ul.goods_list > li._itemSection').each(function () {
const title = $.trim($(this).find('div.info > a.tit').text());
const price = $(this).find('div.info > .price .num').text();
const image = $(this).find('div.img_area img').attr('src');
data.push({ title, price, image })
});
return data;
});
console.log(result);
await browser.close();
})();
app.listen(3000, () => console.log("Express!!!"));
我有一个问题。如果我想从多个页面获取信息,我该怎么做? 例如(第1页,第2页,第3页......)