2015-09-27 64 views
2

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」孩子的對象?

謝謝你的時間。

+0

確保您的輸出是一個對象NOT字符串。然後你可以訪問'your_object.query.results.body'。如果你有字符串輸出:'JSON.parse(your_string)' –

+0

好像看起來會更簡單一些,你可以自己刮掉整個頁面,並使用cheerio將所需的內容拉出來。有了YQL,你必須遞歸遍歷所有的DOM元素作爲一個對象數組 – charlietfl

+0

從來沒有使用過。如果你不介意可以請分享一些示例代碼。謝謝。 – ddesai

回答

0

你需要分析使用JSON.parse()JS對象JSON響應。您的代碼可以像這樣重寫 -

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

我試過,它給不明身份的結果。 – ddesai

+0

我似乎無法達到您指定的網址,因此可能會出現這種情況,您可能會遇到此錯誤 – riyadhalnur

+0

http://www.nationallemonlawcenter.com/United-States-Car-Dealerships/Alabama.aspx - 這裏是網址。感謝您爲複製該問題付出努力。 – ddesai