gpt4 book ai didi

javascript - 如何使用 YQL 检索网页结果?

转载 作者:搜寻专家 更新时间:2023-11-01 05:03:04 24 4
gpt4 key购买 nike

我在使用 javascript 设置一个简单的 html 文件来显示 YQL 查询的结果时遇到困难。

我了解如何在 YQL 控制台中设置选择语句(例如:select title,abstract,url from search.web where query="pizza")。但是我不知道如何在html文件上显示它?

有人可以帮助解释如何显示该语句的结果吗?代码片段将不胜感激!

顺便说一句,我已经阅读了 YQL 文档,但它们有些复杂。

最佳答案

通过客户端 JavaScript 检索 YQL 结果的唯一方法是 JSON-P(或使用其他代理)。这是 YQL 服务的包装器:

function YQLQuery(query, callback) {
this.query = query;
this.callback = callback || function(){};
this.fetch = function() {

if (!this.query || !this.callback) {
throw new Error('YQLQuery.fetch(): Parameters may be undefined');
}

var scriptEl = document.createElement('script'),
uid = 'yql' + +new Date(),
encodedQuery = encodeURIComponent(this.query.toLowerCase()),
instance = this;

YQLQuery[uid] = function(json) {
instance.callback(json);
delete YQLQuery[uid];
document.body.removeChild(scriptEl);
};

scriptEl.src = 'http://query.yahooapis.com/v1/public/yql?q='
+ encodedQuery + '&format=json&callback=YQLQuery.' + uid;
document.body.appendChild(scriptEl);

};
}

用法:

// Construct your query:
var query = "select * from rss where url='somefeed.com' limit 1";

// Define your callback:
var callback = function(data) {
var post = data.query.results.item;
alert(post.title);
};

// Instantiate with the query:
var firstFeedItem = new YQLQuery(query, callback);

// If you're ready then go:
firstFeedItem.fetch(); // Go!!

更多信息: http://james.padolsey.com/javascript/using-yql-with-jsonp/

关于javascript - 如何使用 YQL 检索网页结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1176668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com