Skip to content

Instantly share code, notes, and snippets.

@chrisjhoughton
Created January 31, 2014 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjhoughton/8734772 to your computer and use it in GitHub Desktop.
Save chrisjhoughton/8734772 to your computer and use it in GitHub Desktop.
Generic jQuery-based scraping functions created by the engineering team @QuBiT
// Get a date
// Requires getString and isValidDate
var getDate = function (selectorOrEl) {
// Get text
var text = getString(selectorOrEl);
// Try and get the date, return text failing that
var date = new Date(text);
if (this.isValidDate(date)) {
return date;
} else {
return text;
}
};
// Get a string out of a jquery element
var getString = function(selectorOrEl, $) {
// Check jquery
if (!$ || !$.fn || !$.fn.jquery) {
return null;
}
// Get the $el out of selector, or just reference the $el
var $el;
if (selectorOrEl instanceof $) {
$el = selectorOrEl;
} else {
$el = $(selectorOrEl);
}
// Make sure it exists
if ($el.length === 0) {
return null;
}
// Get text
var text = $el.text();
text = $.trim(text);
return text || "";
};
var isValidDate = function (date) {
if (Object.prototype.toString.call(date) === "[object Date]" ) {
if ( isNaN( date.getTime() ) ) { // date.valueOf() could also work
return false;
}
else {
return true;
}
} else {
return false;
}
};
// A function to to get the number out of a jquery selector or $el
var getNumber = function(selectorOrEl, $) {
// Check jquery
if (!$ || !$.fn || !$.fn.jquery) {
return null;
}
// Get the $el out of selector, or just reference the $el
var $el;
if (selectorOrEl instanceof $) {
$el = selectorOrEl;
} else {
$el = $(selectorOrEl);
}
// Make sure it exists
if ($el.length === 0) {
return null;
}
// Get number out
var text = $el.text();
text = $.trim(text);
var number;
if (!text) {
return null;
} else if (text.match(/free/ig)) {
number = 0;
} else {
var match = text.replace(",", "").match(/([0-9|\.]+)/);
number = (match) ? match[0] : null;
number = (!isNaN(number) && number !== null) ? Number(number) : null;
}
return number;
};
// Get a string out of a jquery element
var getString = function(selectorOrEl, $) {
// Check jquery
if (!$ || !$.fn || !$.fn.jquery) {
return null;
}
// Get the $el out of selector, or just reference the $el
var $el;
if (selectorOrEl instanceof $) {
$el = selectorOrEl;
} else {
$el = $(selectorOrEl);
}
// Make sure it exists
if ($el.length === 0) {
return null;
}
// Get text
var text = $el.text();
text = $.trim(text);
return text || "";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment