-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.js
145 lines (129 loc) · 7.03 KB
/
consumer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const puppeteer = require('puppeteer');
const fs = require('fs');
const stringify = require('csv-stringify');
const slugify = require('slugify');
const Queue = require('bull');
(async () => {
var categoryPagesQueue = new Queue('category-pages-queue');
var browser = await puppeteer.launch();
categoryPagesQueue.process(async (job,done) => {
let category = job.data;
(function scrapeCategoryPage(category){
browser.newPage().then((page) => {
page.goto(category.link,{timeout:15000}).then(() => {
console.log('Successfully navigated to category link: ' + category.link);
// Scrape Category Page
page.$$eval('a.core', productLinkElements => {
const productLinks = [];
productLinkElements.forEach(productLinkElement => productLinks.push(productLinkElement.href));
return productLinks;
}).then(productLinks => {
var iterator = 0;
(function scrapeProductPage() {
function scrapeNextProductPage() {
iterator++;
if(iterator < productLinks.length){
scrapeProductPage();
}else{
page.close();
done();
}
};
let productLink = productLinks[iterator];
page.goto(productLink,{timeout:15000}).then(async () => {
console.log('Successfully navigated to product link: ' + productLink);
// Scrape Product Page
let name = await page.$eval('#jm > main > div > section > div > div.col10 > div.-df.-j-bet > div > h1',titleH1Element => titleH1Element.innerText).catch(error => {
console.log(error);
return '';
});
let brand = await page.$eval('#jm > main > div > section > div > div.col10 > div.-phs > div.-fs14.-pvxs > a', brandLinkElement => brandLinkElement.innerText).catch(error => {
console.log(error);
return '';
});
let description = await page.$eval('#jm > main > div > div.col12 > div > div.markup.-mhm.-pvl.-oxa.-sc', descriptionDivElement => descriptionDivElement.innerHTML.replace(/\r?\n|\r/g,'')).catch(error => {
console.log(error);
return '';
});
let weight = await page.$x("//span[text()='Weight (kg)']").then(async (result) => {
return result.length ? await page.evaluate(async weightElement => {
return Number.parseFloat((await weightElement.parentElement.innerText).substr(13));
}, result[0]) : '';
});
let model = await page.$x("//span[text()='Model']").then(async (result) => {
return result.length ? (await page.evaluate(modelElement => modelElement.parentElement.innerText, result[0])).substr(7) : '';
});
let sku = await page.$x("//span[text()='SKU']").then(async (result) => {
return result.length ? (await page.evaluate(skuElement => skuElement.parentElement.innerText, result[0])).substr(5) : 0;
});
let imageLinks = await page.$$eval('#imgs > a > img', imageLinkElements => {
let imageLinks = '';
for(let i = 0; i < imageLinkElements.length; i++){
if(i == imageLinkElements.length){
imageLinks = imageLinks + imageLinkElements[i].getAttribute('data-src');
}else{
imageLinks = imageLinks + imageLinkElements[i].getAttribute('data-src') + '|';
}
}
return imageLinks;
});
let listingPrice = Number.parseFloat((await page.$eval('div.-hr.-pvs.-mtxs > span', priceSpanElement => priceSpanElement.innerText)).substr(4));
let productData = {
name: name,
slug: slugify(name.toLowerCase()),
active: 'TRUE',
category: slugify(category.name.toLowerCase()),
brand: brand,
description: description,
hasVariant: 'FALSE',
requiresShipping: 'TRUE',
imageLinks: imageLinks,
listingPrice: listingPrice,
weight: weight,
sku: sku,
model: model,
manufacturer: '',
originCountry: '',
tags: '',
minPrice: '',
maxPrice: '',
gtin: '',
gtinType: '',
mpn: '',
};
// Create & Save Category in CSV file
const file = (category.name + '.csv');
stringify([productData],{
header: !(fs.existsSync(file)),
quoted:true,
columns:[
{key:'name', header:'NAME'},{key:'slug', header:'SLUG'},{key:'active', header:'ACTIVE'},
{key:'category',header:'CATEGORY'},{key:'brand',header:'BRAND'},{key:'description',header:'DESCRIPTION'},
{key:'hasVariant',header:'HAS_VARIANT'},{key:'requiresShipping',header:'REQUIRES_SHIPPING'},{key:'imageLinks',header:'IMAGE_LINKS'},
{key:'listingPrice',header:'LISTING_PRICE'},{key:'weight',header:'WEIGHT_(KG)'},{key:'sku',header:'SKU'},
{key:'model',header:'MODEL'},{key:'manufacturer',header:'MANUFACTURER'},{key:'originCountry',header:'ORIGIN_COUNTRY'},
{key:'tags',header:'TAGS'},{key:'minPrice',header:'MINIMUM_PRICE'},{key:'maxPrice',header:'MAXIMUM_PRICE'},
{key:'gtin', header:'GTIN'},{key:'gtinType',header:'GTIN_TYPE'},{key:'mpn',header:'MPN'},
]},(error, csv_string) => {
fs.appendFile(file, csv_string, (err) => {
if (err) throw err;
console.log('Added ' + productData.name + ' to ' + file);
// Exit
scrapeNextProductPage();
});
});
}).catch(error => {
console.log(error);
console.log(`Broken Product Link: ${productLink}`);
scrapeNextProductPage();
});
})();
});
}).catch(error => {
console.log(error);
console.log(`Broken Category Link: ${category.link}`);
});
});
})(category);
});
})();