-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobTitleParser.js
51 lines (44 loc) · 1.62 KB
/
jobTitleParser.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
import axios from 'axios';
import cheerio from 'cheerio';
import { faker } from '@faker-js/faker';
import { FakeBrowser } from 'fakebrowser';
const getJobTitlesByFakeBrowser = async (site) => {
const builder = new FakeBrowser.Builder()
.displayUserActionLayer(true)
.vanillaLaunchOptions({
headless: true,
})
.userDataDir('./fakeBrowserUserData');
const fakeBrowser = await builder.launch();
let jobTitles = [];
try {
const page = await fakeBrowser.vanillaBrowser.newPage();
await page.goto(site.url, {waitUntil: 'domcontentloaded'});
await page.waitForSelector(site.jobTitleSelector);
jobTitles = await page.$$eval(site.jobTitleSelector,
elements=> elements.map(item=>item.textContent))
await fakeBrowser.shutdown();
} catch (error) {
console.error(`===============================Parsing ${site.name} returned ERROR:${error}===============================`);
return [];
}
return jobTitles
}
const getJobTitlesByAxios = async (site) => {
try{
const response = await axios.get(site.url, { headers: { 'User-Agent': faker.internet.userAgent() } });
const html = response.data;
const $ = await cheerio.load(html);
let jobTitles = [];
const jobTitlesElements = await $(site.jobTitleSelector);
for (let i = 0; i < jobTitlesElements.length; i++) {
jobTitles[i] = $(jobTitlesElements[i]).text();
}
return jobTitles;
}
catch (error) {
console.error(`===============================Parsing ${site.name} returned ERROR:${error}===============================`);
return [];
}
}
export { getJobTitlesByFakeBrowser, getJobTitlesByAxios };