forked from sonnysangha/brightdata-amazon-scraper-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_2_parser_code.js
52 lines (51 loc) · 1.81 KB
/
step_2_parser_code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let products = $('[data-component-type="s-search-result"]')
.toArray()
.map((el) => {
let $el = $(el);
let desc_el = $(el).children(".sg-col").eq(1);
let title_el = $(el).find("h2 a.a-text-normal").eq(0);
let name_el = title_el.find("span").eq(0);
let rating_el = $(el).find("span[aria-label*=stars]").eq(0).parent();
let image_el = $(el)
.find('span[data-component-type="s-product-image"] img')
.eq(0);
let price_el = $(el).find(".a-price:not([data-a-strike])").eq(0);
let previous_price_el = $(el).find(".a-price[data-a-strike]").eq(0);
let reviews = rating_el.find("span[aria-label]").eq(1).attr("aria-label");
let parse_price = (el) => {
let price = $(el).find(".a-offscreen").eq(0).text();
return parseFloat(price.replace(/^\D+/, "").replace(/,/g, ""));
};
let feature_section_el = $(el).find(".a-section").eq(-1);
let features = $(feature_section_el)
.find(".a-row")
.toArray()
.map((el) => $(el).text().replace("\n", "").trim());
return {
search: input.search,
title: name_el.text().replace("\n", "").trim(),
url: new URL(
$el
.find('[data-component-type="s-product-image"]')
.find("a")
.attr("href"),
location.href
),
sponsored:
$el
.find(".s-label-popover .s-label-popover-default")
.eq(0)
.text()
.trim()
.toLowerCase() == "sponsored",
rating:
rating_el.find("span[aria-label]").eq(0).attr("aria-label") || null,
reviews: reviews ? +reviews.replace(/\D/, "") : null,
price: parse_price(price_el),
previous_price: parse_price(previous_price_el),
features,
image: image_el.attr("src"),
imageset: image_el.attr("srcset"),
};
});
return { products };