我使用YQL从另一个每天更新的网站上抓取一些数据,但不管我的努力如何,返回的数据至少要等一天,如果不是旧的话。换句话说,我抓不到网站上的实际数据。我认为它被缓存,从我读到我需要使用某种缓存清除技术来强制它获取新数据。这是我的调用代码的核心:
SomeClass.prototype.testfunc = function () {
var _this = this;
var site = "http://www.somesite.shtml";
var xpath = '//table[@id="someId"]/tbody/tr';
var yql = "https://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent("select * from html where url='" + site + "' and xpath='" + xpath + "'") + "&format=json&callback=?&rnd=2";
$.ajax({
url: yql,
dataType: 'json',
cache: false,
success: _this.testFunc
});
SomeClass.prototype.testFunc = function (data) {
if (data != undefined) {
//handle returned data
}
}
我尝试了两种不同的缓存清除技术来检索当前数据:1)" cache:false"在ajax调用中,2)添加了"& rnd =#"到yql字符串的末尾,我在测试中手动将其更改为不同的数字,以查看是否会产生影响。
我得到的数据,但不幸的是它仍然是一天之久。似乎两种方法都不起作用,我想知道我能做些什么来获取新数据。
答案 0 :(得分:1)
您需要在site
var中添加查询字符串以避免缓存,如下所示:
var site = "http://www.somesite.shtml?t=<timestamp>";