-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbeo.js
More file actions
169 lines (134 loc) · 4.41 KB
/
numbeo.js
File metadata and controls
169 lines (134 loc) · 4.41 KB
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
const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const sample = {
guests: 1,
bedrooms: 1,
beds: 1,
baths: 1,
pesosPerNight: 350,
};
let browser;
let arr = [];
async function visitNumbeo(url, page) {
try {
await page.goto(url);
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const totalResults = await $("#content_and_logo > table > tbody").text();
//const totalResultFind = await totalResults.match(
// /(?=History\[edit]).*((?:[^\n][\n]?)+)/
//);
//const totalResultString = totalResultFind[0];
//console.log(totalResultString);
//console.log(totalResults);
return totalResults;
} catch (error) {
console.error(error);
}
}
//selectOption is the dropdown menus option to be selected.
async function selectCityFromListOfCities(url, numberOfOptions) {
const page = await browser.newPage();
try {
var i;
for (i = 0; i < numberOfOptions; i++) {
await page.goto(url, { waitUntil: "networkidle2" });
let selection = await page.evaluate(async (i) => {
await document.querySelector("#city").selectedIndex;
document.querySelector("#city").selectedIndex = i + 1;
await document.querySelector("#city").form.submit();
}, i);
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const totalResults = await $("#content_and_logo > table > tbody").text();
console.log("city info:", totalResults);
const data = {
info: totalResults,
};
return data;
}
} catch (error) {
await page.reload({ waitUntil: "networkidle2" });
console.error(error);
}
await page.close();
}
async function getListOfCities(url, page) {
try {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const citiesList = await $("#city > option")
.map((i, element) => {
const hrefs = $(element).text() + "'";
const hrefsMatch = hrefs.match(/country_result.jsp\?(.*?)\'/);
arr = hrefsMatch;
return hrefs;
})
.get();
var cities = citiesList.filter(function (element, index, array) {
return index % 1 === 0;
});
var finalCityList = cities.map((place) => place.slice(0, -1));
return finalCityList;
} catch (error) {
console.error(error);
}
}
async function getListOfCountries(url, page) {
try {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const countryList = await $("[href]")
.map((i, element) => {
const hrefs =
"https://www.numbeo.com/cost-of-living/" +
$(element).attr("href") +
"'";
const hrefsMatch = hrefs.match(
/https\:\/\/www.numbeo.com\/cost-of-living\/country_result.jsp\?(.*?)\'/
);
arr = hrefsMatch;
return hrefsMatch;
})
.get();
var countries = countryList.filter(function (element, index, array) {
return index % 2 === 0;
});
var finalCountryList = countries.map((place) => place.slice(0, -1));
return finalCountryList;
} catch (error) {
console.error(error);
}
}
async function main() {
browser = await puppeteer.launch({ headless: false });
const countriesUrl = "https://www.numbeo.com/cost-of-living/";
const countriesPage = await browser.newPage();
const citiesPage = await browser.newPage();
//const statsPage = await browser.newPage();
const countries = await getListOfCountries(countriesUrl, countriesPage);
console.log(countries);
//go to specific country page and get cities of it.
for (var i = 0; i < countries.length; i++) {
const cities = await getListOfCities(countries[i], citiesPage);
console.log(cities.length);
//Open up a new page for 'number of city options' times. (cities.lenght -1 )
const cityBrowsers = [];
const arbitraryCityPage = await selectCityFromListOfCities(
countries[i],
cities.length - 1
);
console.log(arbitraryCityPage);
}
/*
const cityStats = await visitNumbeo(
"https://www.numbeo.com/cost-of-living/in/Istanbul",
statsPage
);
console.log("city stats for city:", city, cityStats);
}
*/
}
main();