-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.js
191 lines (163 loc) · 5.45 KB
/
parse.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
/* eslint-disable @typescript-eslint/await-thenable */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
const puppeteer = require('puppeteer');
const fs = require('fs');
const url = 'https://www.obd-codes.com/p22-codes';
const scrap = async (pcode) => {
const browser = await puppeteer.launch({
ignoreHTTPSErrors: true,
headless: true,
executablePath: '/bin/chromium',
});
const startPage = await browser.newPage();
await startPage.setViewport({
width: 1200,
height: 720,
deviceScaleFactor: 1,
});
await startPage.goto('https://www.obd-codes.com/p' + pcode + '-codes');
const DTCLinks = (
await startPage.evaluate(() => {
return Array.from(document.getElementsByTagName('li')).map(
(li) => li.children[0]?.href
);
})
)
.filter(Boolean)
.filter((url) => String(url).indexOf('/p') > -1);
await startPage.close();
for (let index = 0; index <= DTCLinks.length; index++) {
let code = DTCLinks[index];
if (!code) return;
const page = await browser.newPage();
// console.log(code);
await page.goto(code);
const DTCDescription = await page.evaluate(() => {
return document.getElementsByClassName('tcode')[0].innerText;
});
const DTCCode = code.slice(-5);
console.log(`DTC Code ${code.slice(-5)}, Description: ${DTCDescription}`);
const DTCInfo = await page.evaluate(() => {
let meaningIndex = 0;
let endIndex = 0;
const mainChildrends = Array.from(
document.getElementsByClassName('main')[0].children
);
for (let nodeIndex = 0; nodeIndex < mainChildrends.length; nodeIndex++) {
const element = mainChildrends[nodeIndex];
// console.log(element.innerText);
if (String(element.innerText).indexOf('What does that mean') > -1) {
meaningIndex = nodeIndex;
//console.log(element.innerText);
}
if (
element.localName === 'h2' &&
meaningIndex != 0 &&
meaningIndex != nodeIndex
) {
endIndex = nodeIndex;
break;
}
}
const MeaningChilds = mainChildrends.slice(meaningIndex + 1, endIndex);
return MeaningChilds.map((el) => el.innerText).filter(
(tx) => tx.length > 1
);
});
const DTCSymptoms = await page.evaluate(() => {
let meaningIndex = 0;
let endIndex = 0;
const mainChildrends = Array.from(
document.getElementsByClassName('main')[0].children
);
for (let nodeIndex = 0; nodeIndex < mainChildrends.length; nodeIndex++) {
const element = mainChildrends[nodeIndex];
// console.log(element.innerText);
if (String(element.innerText).indexOf('Symptoms') > -1) {
meaningIndex = nodeIndex;
//console.log(element.innerText);
}
if (
element.localName === 'h2' &&
meaningIndex != 0 &&
meaningIndex != nodeIndex
) {
endIndex = nodeIndex;
break;
}
}
const MeaningChilds = mainChildrends.slice(meaningIndex + 1, endIndex);
return MeaningChilds.map((el) => el.innerText).filter(
(tx) => tx.length > 1
);
});
const DTCCauses = await page.evaluate(() => {
let meaningIndex = 0;
let endIndex = 0;
const mainChildrends = Array.from(
document.getElementsByClassName('main')[0].children
);
for (let nodeIndex = 0; nodeIndex < mainChildrends.length; nodeIndex++) {
const element = mainChildrends[nodeIndex];
// console.log(element.innerText);
if (String(element.innerText).indexOf('Causes') > -1) {
meaningIndex = nodeIndex;
//console.log(element.innerText);
}
if (
element.localName === 'h2' &&
meaningIndex != 0 &&
meaningIndex != nodeIndex
) {
endIndex = nodeIndex;
break;
}
}
const MeaningChilds = mainChildrends.slice(meaningIndex + 1, endIndex);
return MeaningChilds.map((el) => el.innerText).filter(
(tx) => tx.length > 1
);
});
/* console.log('DTC INFO - WIP');
console.log(DTCInfo);
console.log('DTC Symptoms - WIP');
console.log(DTCSymptoms);
console.log('DTC Causes - WIP');
console.log(DTCCauses); */
const jsonData = JSON.stringify({
code: DTCCode.toUpperCase(),
description: DTCDescription,
info: DTCInfo,
symptoms: DTCSymptoms,
causes: DTCCauses,
});
fs.writeFile(
'mocks/p' + pcode + '/' + `${DTCCode}.json`,
jsonData,
'utf8',
(err) => {
err = err;
}
);
await page.close();
}
//var xpath = "//a[text()='SearchingText']";
// var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// por pagina:
// Array.from(document.getElementsByClassName("main")[0].children)[3]
// H2 con => What does that mean?
};
void (async () => {
/* await void scrap('23');
await void scrap('24');
await void scrap('25');
await void scrap('26');
await void scrap('27');
await void scrap('28'); */
await void scrap('34');
})();