I'm trying to fetch new products from a supplier website. On their new products page is a bunch of URLs and thumbnails, and clicking a URL takes you to it's product page.
I'm fetching all of the URLs from the page, then want to fetch data from each of those URLs. This works, but it's very slow and i'm not sure how to rewrite it so it's cleaner and faster.
import fetch from "isomorphic-fetch";
import cheerio from "cheerio";
export const fetchNewProducts = async (page) => {
try {
const req = await fetch(
"https://www.supplier.co.uk/newarrivals/?setPerPage=25&search_direction=asc&pageID=" +
page
);
const html = await req.text();
const $ = cheerio.load(html);
let newProducts = [];
for (let i = 1; i < 26; i++) {
let pageSrc = $(
`#product_listing > tbody > #_${i} > td:nth-child(2) > a`
).attr("href");
pageSrc = "https://www.supplier.co.uk" + pageSrc;
const req2 = await fetch(pageSrc);
const html2 = await req2.text();
const $2 = cheerio.load(html2);
let imageSrc = $2(
"#product-main-image .main-image-inner:first-child img"
).attr("src");
const name = $2("#product-details dd:nth-child(2)")
.text();
const brand = $2("#product-details dd:nth-child(4)")
.text();
const price = $2("#product-details dd:nth-child(6)")
.text();
newProducts.push({
name,
imageSrc,
brand,
price,
pageSrc,
});
}
return newProducts;
} catch (err) {}
};
module.exports = {
fetchNewProducts,
};