0

So i have code scraping like this. but this code eror when me save to json file

massage eror = TypeError: Converting circular structure to JSON starting at object with constructor 'Node' property 'children' -> object with constructor 'Array' index 0 -> object with constructor 'Node' property 'parent' closes the circle at JSON.stringify ()

const axios = require('axios');
const cheerio = require('cheerio');
var fs = require('fs');
const url = 'https://icons8.com/line-awesome';

axios(url)
 .then(response => {
   const html = response.data;
   const $ = cheerio.load(html)
   const listIcons = $('.icons-group');
   const list = [];

listIcons.each(function () {
  const title = $(this).find('.icons-title').text();
  const icons = [];
  const panjang = $(this).find('.name');
  for (let i = 0; i < panjang.length; i++) {
    icons.push($(panjang[i]).text());
  }
//   console.log(panjang.length);
//   console.log(panjang[0]);
  list.push({
    title,
    icons,
  });
  console.log(list);
 });
  fs.writeFile('nama.json', JSON.stringify(listIcons, null, 4));
})
.catch(console.error);
Hyper Core
  • 11
  • 3
  • Does this answer your question? [How can I print a circular structure in a JSON-like format?](https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format) – filipe Feb 11 '20 at 08:46
  • Please see https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format. It suggests ways to avoid circular structures in JSON. – filipe Feb 11 '20 at 08:46

1 Answers1

0

Each HTML node is refering somewhere each other html node. just like A node refers its parent then A's parent refers parent of parent. This makes it circular, so this is the reason of your. Just use your need fields like title innerText instead take all node object.