2015-06-27 65 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>