Last active
October 16, 2017 17:27
-
-
Save lap00zza/1b4d1de0baed99087a316bed643d2941 to your computer and use it in GitHub Desktop.
Scrape anime info from MAL
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
const h2 = document.querySelectorAll("h2"); | |
const infoH2 = (() => { | |
for (let i = 0; i < h2.length; i++) { | |
if (h2[i].innerText === "Information") return h2[i]; | |
} | |
})(); | |
// holds the elements between the Information (H2) and the next H2 | |
const elList = []; | |
// start from the element after the H2 | |
let now = infoH2.nextElementSibling; | |
do { | |
elList.push(now); | |
now = now.nextElementSibling; | |
} while (now.tagName !== "H2"); | |
// Rough Serialization | |
const kvmap = elList.map((el) => { | |
try { | |
return { | |
key: el.getElementsByTagName("span")[0].innerText, | |
value: el.innerText | |
}; | |
} catch (e) { | |
return null; | |
} | |
}); | |
const cleanedObj = kvmap.reduce((acc, val) => { | |
if (val) { | |
// key contains a extra `:`. val contains the text contents | |
// of key plus the value. ex: Status:: "Status: Finished Airing", | |
// which we gotta clean up. | |
acc[val.key.slice(0, -1)] = val.value.slice(val.key.length + 1); | |
} | |
return acc; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Details
cleanedObj
Sample Result