While working on a JavaScript scraper for parsing JSON data, I encountered the challenge of separating each column with a value and data. After trying several methods, my code ended up looking like this:
searchMangaFromElement(element) {
var obj = JSON.parse(element.text())
var name = obj.suggestions.map(user => user.value);
var url = obj.suggestions.map(user => user.data);
var thumbnail = `${this.baseUrl}/uploads/manga/${url}/cover/cover_250x350.jpg`
var rank = '0';
return super.manga(name, url, thumbnail, rank);
}
{
"mangas": [
{
"name": [...list of manga names...],
"url": [...list of URLs...],
"thumbnail": "https://zahard.xyz/uploads/manga/th3-b0xer,7854-im-the-grim-reaper,th3-girl-d0wnstairs,...",
"rank": "0"
}
],
"hasNextPage": 1,
"nextPage": 1,
"results": 1
}
I then attempted to improve the code by iterating through the suggestions object in the JSON data:
searchMangaFromElement(element) {
var obj = JSON.parse(element.text())
for (var vals in obj.suggestions) {
var url = obj.suggestions[vals].data;
var name = obj.suggestions[vals].value;
var thumbnail = `${this.baseUrl}/uploads/manga/${url}/cover/cover_250x350.jpg`
var rank = '0';
return super.manga(name, url, thumbnail, rank);
}
}
Furthermore, an alternative method using Cheerio library was implemented to parse specific content as shown below:
{
"mangas": [
{
"name": "10 Years in the Friend Zone",
"url": "/manga/2053/10-years-in-the-friend-zone",
"thumbnail": "https://cdn.mangaworld.in/mangas/5faa0b5f66d2cb714b2b2df9.png?1640316529137?",
"rank": "0"
},
{...more manga entries...}
],
"hasNextPage": true,
"nextPage": 2,
"results": 16
}