Created
January 31, 2014 15:53
-
-
Save chrisjhoughton/8734772 to your computer and use it in GitHub Desktop.
Generic jQuery-based scraping functions created by the engineering team @QuBiT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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