私の環境だと。以下のコードで何度か実施しているとたまに成功します、、、
URL FETCHでコンテンツを解析することは未経験なので、絶対にもっときれいに記載できるはずですが、、、
code.gs
1function test_getDomainIndex() {
2 getDomainIndex('google.com');
3}
4
5function getDomainIndex(domain) {
6 var response = UrlFetchApp.fetch('https://www.google.co.jp/search?hl=ja&q=site:'+domain);
7 var content = response.getContentText();
8 var index = content.
9 match(/<div class=\"sd\" id=\"resultStats\"[^0-9]*[0-9\,]+/)[0].
10 match(/[0-9\,]+/)[0].
11 replace(/\,/g,"");
12 return index;
13}
spreadsheetでは以下を入力します。
=getDomainIndex("domain.com")
実際にはB1セルに以下を入力し、A1セルにドメイン名を入力する感じです。
=getDomainIndex(A1)
UrlFetchApp.fetchで失敗する件について私自身も興味があり、本質問とは本質的には異なる気がするので、別の質問をしました。
UrlFetchApp.fetchで(エラー: 503)が発生することがある。