2015-06-27 30 views
2

我使用YQL從atlatlsoftware.com上的div中提取基本信息。我需要找到地址,電話號碼和電子郵件。使用YQL和jQuery提取數據

我當前的代碼從YQL轉換數據並將其作爲JSON記錄在控制檯中。

var atlatlInfo = $.getJSON("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fatlatlsoftware.com%22%20and%0A%20%20%20%20%20%20xpath%3D'%2F%2F*%5B%40id%3D%22desktop-footer%22%5D%2Fdiv%5B3%5D%2Fdiv%2Ftable%2Ftbody%2Ftr%5B2%5D%2Ftd%5B1%5D'&format=json&diagnostics=true&callback="); 

console.log(atlatlInfo); 

在Chrome控制檯鍵入atlatlInfo.responseJSON.query.results.td.div,我可以得到我需要的數據。當我嘗試執行console.log(atlatlInfo.responseJSON.query.results.td.div)時,我的Chrome控制檯出現「undefined」。

如何獲取我需要使用的數據,用javascript?

回答

0

$.getJSON返回異步結果;當console.log(atlatlInfo);調用時,atlatlInfo可能沒有定義;見How do I return the response from an asynchronous call?。嘗試使用的jQuery.getJSON(url [, data ][, success ])success回調來處理由調用返回data$.getJSON

var atlatlInfo = $.getJSON("https://query.yahooapis.com/v1/public/yql?" 
 
          + "q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fatlatlsoftware.com%22%20and%0A%20%20%20%20%20%20xpath%3D" 
 
          + "'%2F%2F*%5B%40id%3D%22desktop-footer%22%5D%2Fdiv%5B3%5D%2Fdiv%2Ftable%2Ftbody%2Ftr%5B2%5D%2Ftd%5B1%5D'" 
 
          + "&format=json&diagnostics=true&callback=" 
 
          // process results from asynchronous call here 
 
          , function success(data) { 
 
           // do stuff with results 
 
           console.log(data.query.results.td.div); 
 
       });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 
 
</script>