Skip to content

Instantly share code, notes, and snippets.

@lap00zza lap00zza/scrapper.js
Last active Oct 16, 2017

Embed
What would you like to do?
Scrape anime info from MAL
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;
}, {});
@lap00zza

This comment has been minimized.

Copy link
Owner Author

lap00zza commented Oct 16, 2017

Usage Details

Sample Result

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.