-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractionTool.js
66 lines (52 loc) · 1.29 KB
/
extractionTool.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
const { JSDOM } = require("jsdom")
const axios = require('axios')
class EmptyResult extends Error {
constructor(){
super("the result is empty")
}
}
async function receiveData(url){
let options = {
method: 'GET',
url: url
}
let res=await axios.request(options)
return res.data
}
function parseData(data,selector){
let dom = new JSDOM(data)
let document = dom.window.document
let result=document.querySelector(selector)
return result
}
async function extractData(url,querySelector,mode=0,regex){
let html=await receiveData(url)
let parsedData = parseData(html,querySelector)
let result
if(!parsedData){
throw new EmptyResult
}
switch(mode) {
case 0:
result= parsedData.textContent
break
case 1:
result= parsedData.innerHTML
break
default:
throw `\n${mode} is not a valid mode \nPlease select: \n0 - text mode: get the textContent value \n1 - html mode: get the innerHTML value`
}
if(regex){
result=result.match(regex)
}
if (typeof(result)==="object"){
if (result.length===1){
result=result[0]
}
}
return result
}
module.exports={
extractData,
EmptyResult
}