繁体   English   中英

将数据json从网络保存到json文件

[英]Save data json from web to json file

所以我有这样的代码抓取。 但是当我保存到 json 文件时这个代码错误

按摩错误 = 类型错误:将循环结构转换为 JSON,从构造函数为“节点”属性为“子项”的对象开始 -> 构造函数为“数组”索引 0 的对象 -> 构造函数为“节点”属性为“父”的对象在 JSON 处关闭圆圈。字符串化 ()

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);

每个 HTML 节点都在某个地方引用其他 html 节点。 就像 A 节点引用其父节点一样,A 的父节点引用父节点的父节点。 这使它循环,所以这就是你的原因。 只需使用您需要的字段,如title innerText而不是获取所有节点对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM