将数据 json 从 web 保存到 json 文件



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

按摩 eror = 类型错误:将循环结构转换为 JSON 从具有构造函数"节点"的对象开始 属性 'children' -> 带有构造函数 'Array' 的对象 索引 0 -> 具有构造函数"节点"的对象 属性"父"关闭圆圈 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);

每个 HTML 节点都在某个地方引用彼此的 html 节点。 就像 A 节点引用其父节点一样,A 的父节点引用父节点的父节点。这使得它成为循环的,所以这就是你的原因。只需使用您的需求字段(如titleinnerText(即可获取所有节点对象。

最新更新