首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Readability Parser API中的数据

使用Readability Parser API中的数据
EN

Stack Overflow用户
提问于 2014-04-18 22:59:35
回答 1查看 951关注 0票数 0

在Node.js中使用Readability Parser时:

代码语言:javascript
复制
var request = require("request");
request("https://readability.com/api/content/v1/parser?url=http://www.gq.com/sports/profiles/201202/david-diamante-interview-cigar-lounge-brooklyn-new-jersey-nets?currentPage=all&token=7myToken", function(err, resp, body) {
console.log(body);
});

得到这样的文章表示:

代码语言:javascript
复制
{
"content" <div class=\"article-text\">\n<p>I'm idling outside Diamante's, [snip] ...</p></div>",
"domain": "www.gq.com",
"author": "Rafi Kohan",
"url": "http://www.gq.com/sports/profiles/201202/david-diamante-interview-cigar-lounge-brooklyn-new-jersey-nets?currentPage=all",
"short_url": "http://rdd.me/g3jcb1sr",
"title": "Blowing Smoke with Boxing's Big Voice",
"excerpt": "I'm idling outside Diamante's, a cigar lounge in Fort Greene, waiting for David Diamante, and soon I smell him coming. It's late January but warm. A motorcycle growls down the Brooklyn side street,&hellip;",
"direction": "ltr",
"word_count": 2892,
"total_pages": 1,
"date_published": null,
"dek": "Announcer <strong>David Diamante</strong>, the new voice of the New Jersey (soon Brooklyn) Nets, has been calling boxing matches for years. On the side, he owns a cigar lounge in the heart of Brooklyn. We talk with Diamante about his new gig and the fine art of cigars",
"lead_image_url": "http://www.gq.com/images/entertainment/2012/02/david-diamante/diamante-628.jpg",
"next_page_id": null,
"rendered_pages": 1
}

我如何使用这些数据?例如,只使用"word_count"?该代码似乎不起作用:

代码语言:javascript
复制
console.log(body.word_count);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-20 03:59:52

您需要使用JSON.parse(body)string结果转换为对象。

代码语言:javascript
复制
var request = require("request");
request('your-url', function(err, resp, body) {
    var parsedBody = JSON.parse(body);
    console.log(parsedBody.word_count);
});

根据 docs,您还可以在选项中将json设置为true,以使其自动解析json:

代码语言:javascript
复制
var request = require("request");
request({
    url: 'your-url',
    json: true
}, function(err, resp, body) {
    console.log(body);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23163658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档