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] } } }
如何访问正文内容:[对象]?
感谢您的时间。
我修改了以下代码:
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'的儿童对象中的什么?
感谢您的时间。
答案 0 :(得分:0)
您需要使用JSON.parse()
将 JSON 响应解析为 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);
});