Skip to content

Instantly share code, notes, and snippets.

@kaffyzoo
Last active June 17, 2016 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kaffyzoo/4c4b073811df1a96a5b5 to your computer and use it in GitHub Desktop.
Save kaffyzoo/4c4b073811df1a96a5b5 to your computer and use it in GitHub Desktop.
Scraping Google Travel Flights
var flights = $("div:contains('Best flights'):last").parent().children();
if (flights.length === 0) {
flights = $("div:contains('Choose an outbound'):last").parent().parent().children()[1];
flights = $($(flights).children()[0]).children();
}
var avg_time = 0;
var avg_price = 0;
flights.toArray().forEach(function (elm, ind, arr) {
if ($(elm).attr("iti") != undefined) {
var valid_data = ($(elm).children()[0]);
var child_data = $(valid_data).children()[0];
child_data = $(child_data).children()[0];
var price_hooray_finally = $($(child_data).children()[0]).text();
price_hooray_finally = price_hooray_finally.slice(1);
var time = $($(valid_data).children()[2]);
time = $(time).first().text().split(" ");
var time_hours = (time[0] !== "from") ? time[0] : time[1];
var time_minutes = (time[0] !== "from") ? time[1] : time[2];
time = parseInt(time_hours.slice(0,-1)) + parseInt(time_minutes.slice(0,-1))/60;
avg_price += parseInt(price_hooray_finally);
avg_time += time;
}
});
avg_price = avg_price / flights.length;
avg_time = avg_time / flights.length;
console.log("Average Price: ", avg_price);
console.log("Average Time: ", avg_time);
@shorty2hops
Copy link

Thanks Kathy,

I was looking at the code of your calendar map today. How would i just show one month? is there something in the code i can change easily?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment