【问题标题】:Using data from Readability Parser API使用来自 Readability Parser API 的数据
【发布时间】:2014-04-20 11:59:52
【问题描述】:

在 Node.js 中使用 Readability Parser API 时:

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

并获得这样的文章表示:

{
"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”?这段代码好像不行:

console.log(body.word_count);

【问题讨论】:

  • 可能需要解析响应?如果你这样做 console.log(typeof body);string 还是 object
  • 谢谢。它说string

标签: javascript node.js parsing readability


【解决方案1】:

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

var request = require("request");
request('your-url', function(err, resp, body) {
    var parsedBody = JSON.parse(body);
    console.log(parsedBody.word_count);
});

根据the request docs,你也可以在选项中设置jsontrue,让它自动解析json:

var request = require("request");
request({
    url: 'your-url',
    json: true
}, function(err, resp, body) {
    console.log(body);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2021-07-14
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2023-02-12
    相关资源
    最近更新 更多