例外:一天中服务调用太多次:urlfetch [英] Exception: Service invoked too many times for one day: urlfetch
问题描述
我在Google表格中创建了一个脚本,该脚本运行良好,但是一段时间后出现以下错误:例外:一天中服务被调用太多次:urlfetch
I created a script in Google Sheets, which is working well but after a while I'm getting the following error: Exception: Service invoked too many times for one day: urlfetch
我想我一天调用了200-300次该函数,因为我检查过该函数应低于限制.
I think I called the function like 200-300 times in the day, for what I checked it should be below the limit.
我读到我们可以使用缓存来避免此问题,但是不确定如何在我的代码中使用它.
I read we can use cache to avoid this issue but not sure how to use it in my code.
function scrapercache(url) {
var result = [];
var description;
var options = {
'muteHttpExceptions': true,
'followRedirects': false,
};
var cache = CacheService.getScriptCache();
var properties = PropertiesService.getScriptProperties();
try {
let res = cache.get(url);
if (!res) {
// trim url to prevent (rare) errors
url.toString().trim();
var r = UrlFetchApp.fetch(url, options);
var c = r.getResponseCode();
// check for meta refresh if 200 ok
if (c == 200) {
var html = r.getContentText();
cache.put(url, "cached", 21600);
properties.setProperty(url, html);
var $ = Cheerio.load(html); // make sure this lib is added to your project!
// meta description
if ($('meta[name=description]').attr("content")) {
description = $('meta[name=description]').attr("content").trim();
}
}
result.push([description]);
}
}
catch (error) {
result.push(error.toString());
}
finally {
return result;
}
}
请问如何使用这样的缓存来增强脚本?
how can I use cache like this to enhance my script please?
var cache = CacheService.getScriptCache();
var result = cache.get(url);
if(!result) {
var response = UrlFetchApp.fetch(url);
result = response.getContentText();
cache.put(url, result, 21600);
预先感谢
推荐答案
答案:
您可以一起实现 CacheService
和 PropertiesService
,仅在指定的时间后再次检索URL.
Answer:
You can implement CacheService
and PropertiesService
together and only retrieve the URL again after a specified amount of time.
请注意,额外的检索缓存和属性的调用会降低您的功能的运行速度,尤其(如果您执行了数百次).
Be aware that additional calls to retrieving the cache and properties will slow your function down, especially if you are doing this a few hundred times.
由于缓存的值可以是最大值100 KB ,我们将使用 CacheService
来跟踪要检索的URL,而使用 PropertiesService
来存储数据.
As the values of the cache can be a maximum of 100 KB, we will use CacheService
to keep track of which URLs are to be retrieved, but PropertiesService
to store the data.
您可以这样编辑 try
块:
var cache = CacheService.getScriptCache();
var properties = PropertiesService.getScriptProperties();
try {
let res = cache.get(url);
if (!res) {
// trim url to prevent (rare) errors
url.toString().trim();
var r = UrlFetchApp.fetch(url, options);
var c = r.getResponseCode();
// check for meta refresh if 200 ok
if (c == 200) {
var html = r.getContentText();
cache.put(url, "cached", 21600);
properties.setProperty(url, html);
var $ = Cheerio.load(html); // make sure this lib is added to your project!
// meta description
if ($('meta[name=description]').attr("content")) {
description = $('meta[name=description]').attr("content").trim();
}
}
result.push([description]);
}
}
catch (error) {
result.push(error.toString());
}
finally {
return result;
}
希望对您有帮助!
- CacheService类|应用脚本Google Developers
- 类缓存|应用脚本Google Developers
- 类PropertiesService |应用脚本Google Developers
这篇关于例外:一天中服务调用太多次:urlfetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!