Created
April 7, 2020 07:05
-
-
Save corbin-c/aedcb3910cb8c9de5fd57960bb7d58ed to your computer and use it in GitHub Desktop.
bookmarklet to scrape a html table 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
javascript: | |
(async () => { | |
let parentType = (element,type) => { | |
if (element.nodeName.toLowerCase() == type.toLowerCase()) { | |
return element; | |
} else if (element.parentElement != null) { | |
return parentType(element.parentElement,type); | |
} else { | |
return false; | |
} | |
}; | |
let clickInput = (type) => { | |
return new Promise((resolve,reject) => { | |
document.addEventListener("click", (e) => { | |
e = parentType(e.target,type); | |
if (e) { | |
resolve(e); | |
} | |
}); | |
}); | |
}; | |
let getValue = (cell) => { | |
let value = ""; | |
if (cell.innerText == "") { | |
if (cell.querySelector("img") !== null) { | |
value = cell.querySelector("img").getAttribute("alt"); | |
} | |
} else { | |
value = cell.innerText; | |
} | |
return value; | |
}; | |
alert("click a table"); | |
let table = await clickInput("table"); | |
let tableHead = table.querySelector("tr"); | |
let attributes = []; | |
let output = []; | |
[...tableHead.children].map((e,i) => { | |
attributes[i] = getValue(e); | |
}); | |
[...table.querySelectorAll("tr")].slice(1).map(row => { | |
let rowValues = {}; | |
[...row.children].map((e,i) => { | |
rowValues[attributes[i]] = getValue(e); | |
}); | |
output.push(rowValues); | |
}); | |
alert(JSON.stringify(output)) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment