【问题标题】:Exception: Service invoked too many times for one day: urlfetch例外:一天内服务调用次数过多:urlfetch
【发布时间】:2020-07-09 00:41:03
【问题描述】:

我在 Google 表格中创建了一个脚本,该脚本运行良好,但一段时间后我收到以下错误: 异常:一天服务调用次数过多:urlfetch

我想我一天调用了 200-300 次这个函数,因为我检查过它应该低于限制。

我读到我们可以使用缓存来避免这个问题,但不确定如何在我的代码中使用它。

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;
}
 
}

请问如何使用这样的缓存来增强我的脚本?

var cache = CacheService.getScriptCache();
  var result = cache.get(url);
  if(!result) {
    var response = UrlFetchApp.fetch(url);
    result = response.getContentText();
    cache.put(url, result, 21600);

【问题讨论】:

  • I read we can use cache to avoid this issue 你在哪里读到这个?缓存信息与您发出的获取请求的数量无关,因此这本身并不能减轻命中限制。但是,如果您将信息存储在脚本缓存中,则可能不需要每次都进行提取。你怎么打电话给scrapercache(url)url 是什么?
  • 嗨拉法,感谢您的留言。我在这篇文章中读到了关于缓存的信息:stackoverflow.com/questions/46426792/… 我这样调用刮板函数:``` =value(left(REGEXEXTRACT(scraper("gurufocus.com/term/fscore/…),".as."),1)) ``` 我可能有 200 个这样的单元格,所以也许他们调用 URLfetch 的次数太多了,所以在获取数据后将数据存储在缓存中会很好。你知道如何在我的函数中包含缓存元素吗? ? 谢谢

标签: caching google-apps-script google-sheets urlfetch


【解决方案1】:

答案:

您可以同时实现CacheServicePropertiesService,并且只有在指定的时间后才再次检索 URL。

代码更改:

请注意,对检索缓存和属性的额外调用会减慢您的功能,尤其是如果您这样做数百次。

由于缓存的值可以是maximum of 100 KB,因此我们将使用CacheService 来跟踪要检索的URL,但使用PropertiesService 来存储数据。

您可以像这样编辑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;
}

参考资料:

相关问题:

【讨论】:

  • 嗨 Rafa,我修改了公式的 try 代码,就像你说的那样。当我尝试像这样调用函数时:=scrapercache("https://www.gurufocus.com/term/fscore/"&B41&":"&A41&"/Piotroski-F-Score") with B41 = nyse & A41 = ABBV 我收到以下错误:Exception: Argument too large: value 不确定我做错了什么:/ 谢谢
  • 缓存的数据大小限制为 100 KB。改用PropertiesService 可能会更好
  • @Gabriel 我也使用 PropertiesService 更新了我的代码。这个想法是使用CacheService 来标记最近检索到的url,但是为了克服值限制,使用PropertiesService 存储数据。当缓存超时发生时,这将被更新。
  • 谢谢,我用你的建议更新了我的脚本,我保存并运行它没有错误。但是当我像这样在我的 Google 表格中调用它时 =scrapercache("gurufocus.com/term/fscore/nyse:ABBV/Piotroski-F-Score") 我会出现错误消息 #REF!参考不退出。我是否根据您的信息正确修改了脚本?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多