-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (94 loc) · 4.08 KB
/
index.js
File metadata and controls
104 lines (94 loc) · 4.08 KB
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
const puppeteer = require('puppeteer');
const readline = require('readline');
const newCarResults = [];
// you enter the car make and model to start the search
process.stdout.write('Enter the Car Make and Model: ');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function(line) {
const makeAndModel = line.split(' ');
const make = makeAndModel[0];
const model = makeAndModel[1];
rl.close();
getCars(make, model);
});
async function getCars(make, model) {
const message = `Will begin the search for ${make} ${model}`;
console.log(message);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
const url = `https://www.copart.com/lotSearchResults/?free=true&query=${make}%20${model}`;
console.log(url);
await page.goto(url);
await page.waitFor(1500);
await page.click('#lot_year');
await page.waitFor(1500);
// await page.screenshot({ path: 'screenshots/shot.png' });
// gets information about the results that we got back from our search
const resultDetails = await page.evaluate(function() {
//takes a part of the string containing the number of results and then parses it
const dataTableInfo = '#serverSideDataTable_info';
const numberOfResultsString = document.querySelector(dataTableInfo).innerText.split(' ')[5];
const numberOfResults = parseInt(numberOfResultsString.replace(',', ''));
const currentlyDisplaying = document.querySelector('#serverSideDataTable > tbody').childElementCount;
const pages = Math.ceil(numberOfResults / currentlyDisplaying);
return {
currentlyDisplaying: currentlyDisplaying,
numberOfResults: numberOfResults,
pages: pages,
};
});
console.log(resultDetails);
// if it is a really commmon car then we will need to continue on to the next page.
outerloop: for (let i = 0; i < resultDetails.pages; i++) {
console.log(`On page ${i + 1}`);
for (let j = 1; j <= resultDetails.currentlyDisplaying; j++) {
let carDetails = await page.evaluate(function(index) {
const carSelector = `#serverSideDataTable > tbody > tr:nth-child(${index})`;
const carYearSelector = `${carSelector} > td:nth-child(4) > span:nth-child(1)`;
const carMakeSelector = `${carSelector} > td:nth-child(5) > span`;
const carModelSelector = `${carSelector} > td:nth-child(6) > span`;
const damageTypeSelector = `${carSelector} > td:nth-child(12) > span`;
const retailPriceSelector = `${carSelector} > td:nth-child(13) > span`;
const locationSelector = `${carSelector} > td:nth-child(8) > a > span`;
const lotUrlSelector = `${carSelector} > td:nth-child(3) > div > a`;
const carYear = document.querySelector(carYearSelector).innerText;
const carMake = document.querySelector(carMakeSelector).innerText;
const carModel = document.querySelector(carModelSelector).innerText;
const damageType = document.querySelector(damageTypeSelector).innerText;
const retailPrice = document.querySelector(retailPriceSelector).innerText.split(' ')[0];
const location = document.querySelector(locationSelector).innerText;
const lotUrl = document
.querySelector(lotUrlSelector)
.getAttribute('ng-href')
.replace('.', '');
const url = `${window.location.hostname + lotUrl}`;
const info = {
carMake: carMake,
carModel: carModel,
carYear: carYear,
damageType: damageType,
retailPrice: retailPrice,
location: location,
url: url,
};
console.log(info);
return info;
}, j);
if (carDetails.carYear >= 2014) {
newCarResults.push(carDetails);
} else {
break outerloop;
}
console.log(carDetails);
}
await page.click('#serverSideDataTable_next > a');
console.log('Going to the next page, give me a sec.');
await page.waitFor(2000);
}
console.log(`We have ${newCarResults.length} results in total`);
await browser.close();
}