在节点JS中使用YQL

enter code here我正在尝试从网站中提取出于学习目的的内容。 我使用YQL,它给了我JSON( https://developer.yahoo.com/yql/ )。 我以为我正在取得进展,但不幸的是,我无法通过NPM模块获得相同的输出。 以下是我的代码:

 var YQL = require('yql'); new YQL.exec('select * from html where url="http://www.natnlawcenter.com/United-States-Car-Dealerships/Alabama.aspx" ', function(response) { console.log(response); }); 

以下是我的输出:

 { query: { count: 1, created: '2015-09-27T23:51:25Z', lang: 'en-US', results: { body: [Object] } } } 

我如何访问body的内容:[Object]?

谢谢你的时间。

我已经修改了代码如下:

 request({ method: 'GET', url: 'http://www.natlawcenter.com/United-States-Car-Dealerships/Alabama.aspx' }, function(err, response, body) { if (err) return console.error(err); // Tell Cherrio to load the HTML $ = cheerio.load(body); console.log($('td').each(function(i, element){ var a = $(this); console.log(a); })); }); 

以下是我的输出:

 { options: { withDomLvl1: true, normalizeWhitespace: false, xmlMode: false, decodeEntities: true }, _root: { '0': { type: 'root', name: 'root', attribs: {}, children: [Object], next: null, prev: null, parent: null }, options: { withDomLvl1: true, normalizeWhitespace: false, xmlMode: false, decodeEntities: true }, length: 1, _root: [Circular] }, length: 0, prevObject: { options: { withDomLvl1: true, normalizeWhitespace: false, xmlMode: false, decodeEntities: true }, _root: { '0': [Object], options: [Object], length: 1, _root: [Circular] }, length: 0, prevObject: { '0': [Object], options: [Object], length: 1, _root: [Circular] } } } [Function] [Function] [Function] [Function] [Function] { '0': { type: 'tag', name: 'td', attribs: { valign: 'top', width: '999' }, children: [ [Object], [Object] ], next: { data: '\r\n\t\t\t\t\t\t\t\t', type: 'text', next: null, prev: [Circular], parent: [Object] }, prev: { data: '\r\n\t\t\t', type: 'text', next: [Circular], prev: null, parent: [Object] }, parent: { type: 'tag', name: 'tr', attribs: {}, children: [Object], next: [Object], prev: [Object], parent: [Object] } }, ------------------------------- '188': { type: 'tag', name: 'td', attribs: { width: '25%', icobalt: 'System.Web.UI.ITemplate' }, children: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ], next: { type: 'tag', name: 'td', attribs: [Object], children: [Object], next: [Object], prev: [Circular], parent: [Object] }, prev: { type: 'tag', name: 'tr', attribs: [Object], children: [Object], next: [Circular], prev: [Object], parent: [Object] }, parent: { type: 'tag', name: 'tbody', attribs: {}, children: [Object], next: null, prev: null, parent: [Object] } }, 

我怎样才能访问例如'188'的儿童对象?

谢谢你的时间。

您需要使用JSON.parse()parsingJS对象的JS对象。 你的代码可以像我们这样重写,

 request({ method: 'GET', url: 'http://www.natlawcenter.com/United-States-Car-Dealerships/Alabama.aspx' }, function(err, response, body) { if (err) return console.error(err); if (response.statusCode === 200 && body) return JSON.parse(body); });