-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzomato.js
More file actions
247 lines (201 loc) · 6.37 KB
/
zomato.js
File metadata and controls
247 lines (201 loc) · 6.37 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const sample = {
guests: 1,
bedrooms: 1,
beds: 1,
baths: 1,
pesosPerNight: 350,
};
let browser;
async function openRestaurant(url, page) {
try {
await page.goto(url, { waitUntil: "load", timeOut: 0 });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const restaurantTitle = await $(
"#root > main > div > section > section > section > h1"
).text();
const restaurantCategory = await $(
"#root > main > div > section > section > section > section > div"
).text();
const openHours = await $(
"#root > main > div > section > section > section > section > span"
).text();
const knownFor = await $(
"#root > main > div > section > section > section > article > section > p"
).text();
const averagePrice = await $(
"#root > main > div > section > section > section > article > section > p"
).text();
const score = await $(
"#root > main > div > section > section > section > section > div > p"
).text();
const telephoneNumber = await $(
"#root > main > div > section > section > article > p"
).text();
const keywords = await $("#root > main > div > div > div > div").text();
const data = {
title: restaurantTitle,
category: restaurantCategory,
score: score,
open: openHours,
knownFor: knownFor,
averagePrice: averagePrice,
phone: telephoneNumber,
keywords: keywords,
};
return data;
} catch (error) {
console.error(error);
}
}
async function browseRestaurants(url, page) {
try {
await page.goto(url, { waitUntil: "load", timeOut: 0 });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const restaurante = await $(
"#orig-search-list > div > div.content > div > article > div.pos-relative.clearfix > div > div > div > div > a"
)
.map((i, element) => {
const hrefs = $(element).attr("href") + "'";
const hrefsMatch = hrefs.match(/catalog\/(.*?)\'/);
arr = hrefsMatch;
return hrefs;
})
.get();
var restaurants = restaurante.map((place) => place.slice(0, -1));
return restaurants;
} catch (error) {
console.error(error);
}
}
async function openCity(url, page) {
try {
await page.goto(url, { waitUntil: "load", timeOut: 3000 });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const subCities = await $("[href]")
.map((i, element) => {
const hrefs = $(element).attr("href") + "'";
const hrefsMatch = hrefs.match(/catalog\/(.*?)\'/);
arr = hrefsMatch;
return hrefs;
})
.get();
var sc = subCities.map((place) => place.slice(0, -1));
var justSubCities = sc.filter((element) => element.split("/").length === 6);
var subc = justSubCities.filter(
(element) => element.endsWith("/") === false
);
var regexMatched = subc.filter((element) => element.match(/(.*?)dine-out/));
var regexMatched2 = subc.filter((element) => element.match(/(.*?)gold/));
var regexMatched3 = subc.filter((element) =>
element.match(/(.*?)drinks-and-nightlife/)
);
var regexMatched4 = subc.filter((element) =>
element.match(/(.*?)order-food-online\?delivery_subzone=\d+/)
);
//All subc - regexMatched = subcts ( we made 4 filters ( filtered out 4 regex serially.))
var subcts = subc.filter((x) => !regexMatched.includes(x));
var subcts2 = subcts.filter((x) => !regexMatched2.includes(x));
var subcts3 = subcts2.filter((x) => !regexMatched3.includes(x));
var subcts4 = subcts3.filter((x) => !regexMatched4.includes(x));
return subcts4;
} catch (error) {
console.error(error);
}
}
async function browseCountry(url, page) {
try {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const cities = await $("[href]")
.map((i, element) => {
const hrefs = $(element).attr("href") + "'";
const hrefsMatch = hrefs.match(/catalog\/(.*?)\'/);
arr = hrefsMatch;
return hrefs;
})
.get();
var citylist = cities.map((place) => place.slice(0, -1));
var justCities1 = citylist.filter(
(element) => element.endsWith("/") === false
);
var justCities = justCities1.filter(
(element) => element.split("/").length > 4
);
justCities.pop();
justCities.pop();
return justCities;
} catch (error) {
console.error(error);
}
}
async function main() {
browser = await puppeteer.launch({ headless: false });
const mainPageUrl = "https://zomato.com/";
const browseCountryPage = await browser.newPage();
const cityPage = await browser.newPage();
const restaurantsPage = await browser.newPage();
const thatRestaurantPage = await browser.newPage();
const countries = [
"india",
"australia",
"brasil",
"canada",
"chile",
"czech-republic",
"indonesia",
"ireland",
"italy",
"lebanon",
"malaysia",
"newzealand",
"philippines",
"poland",
"portugal",
"qatar",
"singapore-country",
"slovakia",
"southafrica",
"srilanka",
"turkey",
"uae",
"uk",
"united-states",
];
for (var i = 0; i < countries.length; i++) {
const cities = await browseCountry(
mainPageUrl + countries[i],
browseCountryPage
);
//console.log({ countryId: i, cities: cities });
for (var j = 0; j < cities.length; j++) {
const subcities = await openCity(cities[j], cityPage);
//console.log({ countryId: i, cityId: j, city: subcities });
for (var k = 0; k < subcities.length; k++) {
const restaurants = await browseRestaurants(
subcities[k],
restaurantsPage
);
for (var t = 0; t < restaurants.length; t++) {
const restaurant = await openRestaurant(
restaurants[t],
thatRestaurantPage
);
console.log({
country: countries[i],
cityUrl: cities[j],
subcityUrl: subcities[k],
restaurantUrl: restaurants[t],
restaurant: restaurant,
});
}
}
}
}
}
main();