-
-
Save dvdsmpsn/4acc43cfe4dbfb113038 to your computer and use it in GitHub Desktop.
Using Chrome DevTools to scrape HTML table and convert to JSON.
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
// **************************************************** | |
// USING JQUERY TO CONVERT HTML TABLE INTO JSON | |
// by Derek from www.cacheflow.ca | |
// Watch the video: http://youtu.be/DPOOIOU0zVA | |
// **************************************************** | |
// Step 1: Create keys for JSON object | |
var cols = $("#tableID thead tr th").map(function(){ | |
return $(this).text(); | |
}); | |
// eliminate the extraneous strings from cols | |
var headers = cols.splice(10, cols.length); | |
// Fetch the data from the table body | |
var tableObject = $("#tableID tbody tr.tableClass").map(function(i){ | |
var row = {}; | |
$(this).find('td').each(function(i){ | |
var rowName = headers[i]; | |
row[rowName] = $(this).text(); | |
}); | |
return row; | |
}).get(); | |
// convert object to JSON | |
JSON.stringify(tableObject); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment