使用 YQL 和 jQuery 拉取数据

Pulling data with YQL and jQuery

我正在使用 YQL 从 atlatlsoftware.com 上的 div 中提取基本信息。我需要找到地址、phone 号码和电子邮件。

我当前的代码转换来自 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 获取我需要使用的数据?

$.getJSON returns 一个异步结果;其中 atlatlInfo 在调用 console.log(atlatlInfo); 时可能未定义;请参阅 How do I return the response from an asynchronous call?. Try utilizing success callback of jQuery.getJSON( url [, data ][, success ] ) 以处理调用 $.getJSON

返回的 data

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>