-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmazonPriceSearchEngine2.ts
56 lines (49 loc) · 2.02 KB
/
AmazonPriceSearchEngine2.ts
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
import { SearchEngineBase } from './SearchEngineBase';
import { SearchResult } from '..';
import { ISearchResult } from '../../Interfaces/index';
const cheerio = require('cheerio');
export class AmazonPriceSearchEngine2 extends SearchEngineBase {
/**
* Scrapes amazon for products based on your search query
* @param {string} searchTerm
* @returns {ISearchResult[]}
*/
async search(searchTerm: string): Promise<ISearchResult[]> {
const baseUrl = 'https://www.amazon.de';
const $ = await this.requestWebsite(`${baseUrl}/s?k=`, searchTerm);
let titles: any[] = [];
let prices: any[] = [];
let links: any[] = [];
let ratings: any[] = [];
let thumbnails: any[] = [];
let searchResultDivs = $('.s-result-item');
for (let i = 0; i < searchResultDivs.length; i++) {
const thisDiv = searchResultDivs[i];
const htmlOfThisDiv = $.html(thisDiv);
const $detail = cheerio.load(htmlOfThisDiv, {
xmlMode: true
});
if ($detail('.s-title-instructions-style').text() && $detail('.a-price-whole').text()) {
titles.push($detail('.s-title-instructions-style').text().trim());
prices.push($detail('.a-price-whole').text().trim());
links.push(baseUrl + $detail(".a-link-normal[title='product-image']").attr('href'));
ratings.push($detail('.a-icon-alt').text().trim());
thumbnails.push($detail('.s-image').attr('src'));
}
}
let result: ISearchResult[] = [];
titles.forEach((title, index) => {
result.push(
new SearchResult(
this.constructor.name,
links[index],
title,
this.grabFirstPartAsNumber(ratings[index]),
this.grabFirstPartAsNumber(prices[index]),
thumbnails[index]
)
);
});
return result;
}
}