Skip to content

Commit 5256524

Browse files
committed
refactor: use .get() with jQuery and cheerio
1 parent 6e90d93 commit 5256524

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

sources/academy/webscraping/puppeteer_playwright/executing_scripts/extracting_data.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ Now, since we're able to use jQuery, let's translate our vanilla JavaScript code
127127
await page.addScriptTag({ url: 'https://code.jquery.com/jquery-3.6.0.min.js' });
128128

129129
const products = await page.evaluate(() => {
130-
return Array.from($('.product-item').map(function () {
130+
const productCards = $('.product-item');
131+
return productCards.map(function () {
131132
const card = $(this);
132133
const name = card.find('.product-item__title').text();
133134
const price = card.find('.price').contents().last().text();
134135
return { name, price };
135-
}));
136+
}).get();
136137
});
137138

138139
console.log(products);
@@ -216,12 +217,13 @@ Now, to loop through all of the products, we'll make use of the `$` object and l
216217
```js
217218
const $ = load(await page.content());
218219

219-
const products = Array.from($('.product-item').map(function () {
220+
const productCards = $('.product-item');
221+
const products = productCards.map(function () {
220222
const card = $(this);
221223
const name = card.find('.product-item__title').text();
222224
const price = card.find('.price').contents().last().text();
223225
return { name, price };
224-
}));
226+
}).get();
225227

226228
console.log(products);
227229
```
@@ -244,12 +246,13 @@ await page.goto('https://warehouse-theme-metal.myshopify.com/collections/sales')
244246

245247
const $ = load(await page.content());
246248

247-
const products = Array.from($('.product-item').map(function () {
249+
const productCards = $('.product-item');
250+
const products = productCards.map(function () {
248251
const card = $(this);
249252
const name = card.find('.product-item__title').text();
250253
const price = card.find('.price').contents().last().text();
251254
return { name, price };
252-
}));
255+
}).get();
253256

254257
console.log(products);
255258

@@ -270,12 +273,13 @@ await page.goto('https://warehouse-theme-metal.myshopify.com/collections/sales')
270273

271274
const $ = load(await page.content());
272275

273-
const products = Array.from($('.product-item').map(function () {
276+
const productCards = $('.product-item');
277+
const products = productCards.map(function () {
274278
const card = $(this);
275279
const name = card.find('.product-item__title').text();
276280
const price = card.find('.price').contents().last().text();
277281
return { name, price };
278-
}));
282+
}).get();
279283

280284
console.log(products);
281285

0 commit comments

Comments
 (0)