【发布时间】:2017-09-30 16:08:28
【问题描述】:
我想通过 node.js http 请求从 github.com/profile_name 获取 svg 元素内的数据属性,并将其解析为 JSON 数据。
<rect class="day" width="10" height="10" x="-36" y="10" fill="#ebedf0" data-count="0" data-date="2017-09-03"></rect>
输出将是: “天”:[“天”:2017-09-03, “计数”:“10”]
我可以通过简化的 HTTP 请求获取正文,但我不知道如何解析正文并制作 Json 文件。
const request = require('request');
request('http://www.github.com/acgrdumlu', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
}
});
【问题讨论】:
-
如果你得到的数据是一个普通的 HTML 页面并且你想要解析它,你需要使用一个 HTML 解析器,比如cheerio 来获取一个特定的内容片段.一旦cheerio 解析了它,您就可以使用cheerio 的DOM 搜索方法(类似于jQuery)来查找您想要的特定元素并获取它的属性。
标签: javascript json node.js svg httprequest