enter code here
我試圖從學習目的中提取出網站的內容。我使用YQL,它給了我JSON回(https://developer.yahoo.com/yql/)。我以爲我正在取得進展,但不幸的是,我無法通過NPM模塊獲得相同的輸出。以下是我的代碼:在節點JS中使用YQL
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」孩子的對象?
謝謝你的時間。
確保您的輸出是一個對象NOT字符串。然後你可以訪問'your_object.query.results.body'。如果你有字符串輸出:'JSON.parse(your_string)' –
好像看起來會更簡單一些,你可以自己刮掉整個頁面,並使用cheerio將所需的內容拉出來。有了YQL,你必須遞歸遍歷所有的DOM元素作爲一個對象數組 – charlietfl
從來沒有使用過。如果你不介意可以請分享一些示例代碼。謝謝。 – ddesai