-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
257 lines (240 loc) · 8.97 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import puppeteer from "puppeteer";
import { appendFileSync } from "fs";
class Job {
constructor(job_title = "Job Title", location = "Location", address = "Address", job_id = "Job ID", url = "URL", job_description = "Job Description",
shifts = "Shifts", job_level = "Job Level", travel = "Travel", salary = "Salary", responsibilities = "Responsibilities") {
this.job_title = job_title;
this.location = location;
this.address = address;
this.job_id = job_id;
this.url = url;
this.job_description = job_description;
this.shifts = shifts;
this.job_level = job_level;
this.travel = travel;
this.salary = salary;
this.responsibilities = responsibilities;
}
saveAsCSV() {
var csv = `"${this.job_title}","${this.location}","${this.address}","${this.job_id}",${this.url},"${this.job_description}","${this.shifts}","${this.job_level}","${this.travel}","${this.salary}","${this.responsibilities}"\n`;
try {
appendFileSync("./jobs.csv", csv);
} catch (err) {
console.error(err);
}
}
}
const startApp = (csvData) => {
var header = new Job();
header.saveAsCSV();
for (var row of csvData) {
var job = new Job(row['job_title'], row['location'], row['address'], row['job_id'], row['url'], row['job_description'], row['shifts'], row['job_level'], row['travel'], row['salary'], row['responsibilities']);
job.saveAsCSV();
}
}
const getQuotes = async () => {
// Start a Puppeteer session with:
var browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
});
// Open a new page
var page = await browser.newPage();
// On this new page:
await page.goto("https://jobs.walgreens.com/en/search-jobs/", {
waitUntil: "load",
timeout: 0,
});
await page.waitForSelector('#close-modal');
await page.evaluate(() => {
document.querySelector('#close-modal').click();
});
await page.waitForSelector('.filter-checkbox[data-display="United States"]');
await page.evaluate(() => {
document.querySelector('.filter-checkbox[data-display="United States"]').click();
});
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.querySelector('.filter-checkbox[data-display="Pharmacist"]').click();
});
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.querySelector('.filter-checkbox[data-display="Pharmacy Tech"]').click();
});
await page.waitForTimeout(2000);
var state_num = await page.evaluate(() => {
return document.querySelectorAll('section[data-filter-id="3"] li').length;
});
var states = await page.evaluate(() => {
return document.querySelectorAll('section[data-filter-id="3"] li .filter-checkbox');
});
console.log(state_num);
var result = [];
for (var i = 1; i <= state_num; i++){
var flag = await page.evaluate((index) => {
var temp = document.querySelector('section[data-filter-id="3"] li:nth-child(' + index + ') .filter-checkbox');
if (!temp){
return true;
}
if (!temp.checked){
temp.click();
}
return false;
}, i);
if (flag){
break;
}
await page.waitForTimeout(2000);
if (i != 1){
await page.evaluate((index) => {
var temp = document.querySelector('section[data-filter-id="3"] li:nth-child(' + index + ') .filter-checkbox');
if (temp.checked){
temp.click();
return temp.getAttribute('data-display');
}
}, i-1);
}
await page.waitForTimeout(2000);
var job1_flag = await page.evaluate(() => {
var temp = document.querySelector('.filter-checkbox[data-display="Pharmacist"]');
if (temp){
if (!temp.checked){
temp.click();
return 2;
}
return 1;
}
return 0;
});
if (job1_flag == 2){
await page.waitForTimeout(2000);
}
var job2_flag = await page.evaluate(() => {
var temp = document.querySelector('.filter-checkbox[data-display="Pharmacy Tech"]');
if (temp){
if (!temp.checked){
temp.click();
return 2;
}
return 1;
}
return 0;
});
if (job2_flag == 2){
await page.waitForTimeout(2000);
}
if (job1_flag != 0 || job2_flag != 0){
var page_num = await page.evaluate(() => {
var pagination = document.querySelector('.pagination-total-pages');
if (pagination){
return parseInt(pagination.innerText.substr(2));
}
else{
return 1;
}
});
console.log(i, page_num);
for (var j = 0; j < page_num; j++){
result = result.concat(await extractedEvaluateCall(page));
if (j !== page_num - 1) {
await page.waitForSelector(".pagination > .pagination-paging > .next");
await page.evaluate(() => {
var next_btn = document.querySelector('.pagination > .pagination-paging > .next');
if (next_btn){
next_btn.click();
}
});
await page.waitForSelector("#search-results-list:not(.loading)");
}
}
}
}
var final_result = [];
var lll = 0;
for (var row of result){
await page.goto(row['url'], {
waitUntil: "load",
timeout: 0,
});
var { address, job_id, job_description, shifts, job_level, travel, salary, responsibilities } = await extractedJobDetail(page);
final_result.push({
'job_title': row['job_title'],
'location': row['location'],
'address': address,
'job_id': job_id,
'url': row['url'],
'job_description': job_description,
'shifts': shifts,
'job_level': job_level,
'travel': travel,
'salary': salary,
'responsibilities': responsibilities,
})
if (lll > 50){
break;
}
lll++;
}
// Close the browser
await browser.close();
startApp( final_result );
};
async function extractedEvaluateCall(page) {
// Get page data
var quotes = await page.evaluate(() => {
var rows = document.querySelectorAll(".branded-list__list-item");
return Array.from(rows).map((row) => {
var url = row.querySelector("a").href;
var job_title = row.querySelector("h2").innerText;
var location = row.querySelector("span").innerText;
return { job_title, url, location };
});
});
return quotes;
}
async function extractedJobDetail(page){
var result = await page.evaluate(() => {
var temp = document.querySelectorAll('.job-id:not(.copy--blue)');
var job_id = temp[0].innerText.replace('Job ID ', ''); // Job ID
var job_type = ''; // Job Type
if (temp.length == 2){
job_type = temp[1].innerText.replace('Job Type: ', '');
}
var address = document.querySelector('.jd-address').innerText; // Address
temp = document.querySelectorAll('.ats-description');
var job_objectives = ''; // Job Objectives
var responsibilities; // Responsibilities
var section = document.querySelectorAll('#anchor-overview ul > li > p');
var shifts = ''; // Shifts
var job_level = ''; // Job Level
var travel = ''; // Travel
var salary = ''; // Salary
if (section){
shifts = section[0].innerText.replace('Shifts: ', '');
job_level = section[1].innerText.replace('Job Level: ', '');
travel = section[2].innerText.replace('Travel: ', '');
salary = section[3].innerText.replace('Salary: ', '');
}
return { job_id, job_type, address, job_objectives, responsibilities, shifts, job_level, travel, salary };
});
return result;
}
const getScreenShot = async () => {
var browser = await puppeteer.launch({
headless: true,
defaultViewport: null,
});
// Open a new page
var page = await browser.newPage();
await page.goto("https://www.w3schools.com/", {
waitUntil: "load",
timeout: 0,
});
await page.evaluate(() => {
window.scrollTo(0, document.body.scrollHeight);
});
await page.screenshot({ path: 'image1.png', clip: { x: 0, y: 0, width: 1920, height: 10000 } });
await browser.close();
}
// getScreenShot();
getQuotes();