Skip to content

Instantly share code, notes, and snippets.

@lap00zza
Last active October 16, 2017 17:27
Show Gist options
  • Save lap00zza/1b4d1de0baed99087a316bed643d2941 to your computer and use it in GitHub Desktop.
Save lap00zza/1b4d1de0baed99087a316bed643d2941 to your computer and use it in GitHub Desktop.
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
Copy link
Author

Usage Details

Sample Result

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment