Last active
April 26, 2022 14:12
-
-
Save techhjork/be1d8973330f81c22ae12429acafb8f6 to your computer and use it in GitHub Desktop.
Puppeteer Ecommerce Products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer') | |
const paginationInfo = []; | |
const url = "https://www.eichholtz.com/en/collection/new/new-arrivals.html?" | |
async function scrape() { | |
const browser = await puppeteer.launch({}) | |
const page = await browser.newPage() | |
const page2 = await browser.newPage() | |
await page.goto(`${url}p=1`) | |
for(i=1;i<=3;i++){ | |
var perPageProduct = await page.waitForSelector("#toolbar-amount .toolbar-number:nth-child("+i+")"); | |
var text = await page.evaluate(element => parseInt(element.textContent), perPageProduct) | |
paginationInfo.push(text) | |
console.log(paginationInfo) | |
} | |
let noOfPages = Math.ceil(paginationInfo[2]/paginationInfo[1]); | |
for(i = 1; i <= noOfPages; i++){ | |
await page.goto(`${url}p=${i}`); | |
var elproduct_Start = await page.waitForSelector("#toolbar-amount .toolbar-number:nth-child(1)") | |
var elproduct_end = await page.waitForSelector("#toolbar-amount .toolbar-number:nth-child(2)") | |
var product_Start = await page.evaluate(element => parseInt(element.textContent), elproduct_Start) | |
var product_end = await page.evaluate(element => parseInt(element.textContent), elproduct_end) | |
var numberOfProduct = product_end-product_Start | |
// for(product=1;product<=numberOfProduct;product++){ | |
// var elproduct_image = await page.$$(".product-image-photo") | |
// var link = await page.waitForSelector(".product-item-link") | |
// var linkText = await page.evaluate(element => element.textContent, link) | |
// console.log(linkText) | |
// } | |
async function fetchProduct(productLink){ | |
console.log(productLink) | |
await page2.goto(productLink) | |
const productInfo = await page2.$$eval("#maincontent", (productInfo) => { | |
return { | |
title:product.querySelector("h1.page-title").textContent | |
} | |
}) | |
return productInfo; | |
} | |
const res = await page.$$eval(".product-item-info", (productInfo) => | |
productInfo.map(async (product) => { | |
const productLink = product.querySelector(".product.photo.product-item-photo").href; | |
return fetchProduct(productLink) | |
}), | |
); | |
console.log(res) | |
} | |
browser.close() | |
} | |
scrape() | |
// const res = await page.$$eval(".product-item-info", (productInfo) => | |
// productInfo.map((product) => { | |
// return { | |
// url: product.querySelector(".product.photo.product-item-photo").href, | |
// img: product.querySelector(".product-image-photo").src, | |
// productSku: product.querySelector(".product-sku-value").innerText, | |
// productSizes: product.querySelector(".product-item-link").innerText, | |
// }; | |
// }), | |
// ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment