-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scraper.js
60 lines (54 loc) · 1.63 KB
/
Scraper.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
import request from "request-promise";
import cheerio from "cheerio";
import fs from "fs";
import {Parser as json2CsvParser} from "json2csv";
import csvParser from "csv-parser";
// Pure functions:
// const scrapeWebsite = async (uri, headers={}, contentType="json") => {
// const response = await request(
// {
// uri: uri,
// headers: headers,
// [contentType]: true
// });
// return response;
// }
const scrapeWebsite = (headers={}) => {
return (contentType="json") => {
return async (uri) => {
const response = await request(
{
uri: uri,
headers: headers,
[contentType]: true
});
return response;
}
}
}
const scrape2Html = (scrapedCode) => {
let $ = cheerio.load(scrapedCode);
return $;
}
const writeInCSVFile = (filePath, charEncoding="utf-8") => {
return (data) => {
let j2cp = new json2CsvParser();
let csvData = j2cp.parse(data);
// fs.writeFileSync(filePath, csv, charEncoding);
fs.writeFile(
filePath ,
csvData,
charEncoding,
err => {
if (err) console.log("err:", err)
else console.log("CSV file is Written successfully")
}
)
}
}
const readFromCSVFile = (file) => {
fs.createReadStream(file)
.pipe(csvParser())
.on("data", data => console.log(data))
}
export { scrapeWebsite, scrape2Html, writeInCSVFile, readFromCSVFile }