forked from marshdugan/Season-All
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper-function.js
84 lines (66 loc) · 3.07 KB
/
scraper-function.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
// how can I async await the results so they're in order?
// how do we know that the requests are complete? Therefore I can organize the array in alphabetical order. A nested async await seems like the answer, but I don't know how to do that
/*
setTimeout(() => {
// Resolve the promise
resolve(console.log('hello'));
}, 1000);
*/
let cheerio = require("cheerio");
let request = require("request");
let fs = require("fs");
let queryURL1 = "http://nhlr.org/lookouts/us"
fs.writeFile("index.json", "", function(error) {
if (error) throw error
});
// let emptyArray = [];
// let ObjectsToCsv = require('objects-to-csv');
// make ajax call to main 200 link URL
// ajax.get("/whatever")
request("http://nhlr.org/lookouts/us", (error, response, html) => {
if (!error && response.statusCode == 200) {
// console.log(html);
let $ = cheerio.load(html);
$(".tablewhitegrid tr td a").each(function() {
// console.log($(this).attr("href"))
let queryURL2 = $(this).attr("href");
request(`http://nhlr.org${queryURL2}`, (error, response, html) => {
if (!error && response.statusCode == 200) {
let $ = cheerio.load(html);
let name = $(".tablewhitegridnoheader tr:contains('name') td:nth-of-type(2)").text().trim()
let location = $(".tablewhitegridnoheader tr:contains('Location') td:nth-of-type(2)").text().trim();
let coordinates = $(".tablewhitegridnoheader tr:contains('Coordinates') td:nth-of-type(2)").text().trim().split(" ").join("");
let elevation = $(".tablewhitegridnoheader tr:contains('Elevation') td:nth-of-type(2)").text().trim();
let built = $(".tablewhitegridnoheader tr:contains('Built') td:nth-of-type(2)").text().trim();
let htmlObject = {location, coordinates, elevation, built};
// emptyArray.push(htmlObject);
fs.appendFile("index.json", JSON.stringify(htmlObject), function(error) {
if (error) throw error
});
// console.log(location);
// console.log(coordinates);
// console.log(elevation);
// console.log(built);
console.log(htmlObject)
};
});
// console.log(emptyArray)
})
}
});
/*
// How to teell the browser that this function is asynchronous
async function myFunc() {
// Await for the promise to resolve
await new Promise((resolve) => {
setTimeout(() => {
// Resolve the promise
resolve(console.log('hello'));
}, 3000);
});
// Once the promise gets resolved continue on
console.log('hi');
}
// Call the function
myFunc();
*/