質問するログイン新規登録

回答編集履歴

3

誤記修正

2017/02/04 16:03

投稿

hikochang
hikochang

スコア648

answer CHANGED
@@ -6,7 +6,7 @@
6
6
  }
7
7
 
8
8
  function getDomainIndex(domain) {
9
- var response = UrlFetchApp.fetch('https://www.google.co.jp/search?hl=ja&q='+domain);
9
+ var response = UrlFetchApp.fetch('https://www.google.co.jp/search?hl=ja&q=site:'+domain);
10
10
  var content = response.getContentText();
11
11
  var index = content.
12
12
  match(/<div class=\"sd\" id=\"resultStats\"[^0-9]*[0-9\,]+/)[0].

2

UrlFetchApp\.fetchで失敗する件について私自身も興味があり、本質問とは本質的には異なる気がするので、別の質問をしました。

2017/02/04 16:03

投稿

hikochang
hikochang

スコア648

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

1

正規表現修正\[0-9\] => \[0-9\\,\]

2017/02/04 16:00

投稿

hikochang
hikochang

スコア648

answer CHANGED
@@ -4,7 +4,7 @@
4
4
  function getIndexNumber(domain) {
5
5
  var response = UrlFetchApp.fetch('https://www.google.co.jp/search?q=site%3A'+domain);
6
6
  var html = response.getContentText();
7
- var index = html.match(/\=\"resultStats\">約\s[0-9]+(?=\s件)/)[0].match(/[0-9]+/)[0];
7
+ var index = html.match(/\=\"resultStats\">約\s[0-9\,]+(?=\s件)/)[0].match(/[0-9\,]+/)[0];
8
8
  return index;
9
9
  }
10
10
  ```