回答編集履歴

3

誤記修正

2017/02/04 16:03

投稿

hikochang
hikochang

スコア648

test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  function getDomainIndex(domain) {
16
16
 
17
- var response = UrlFetchApp.fetch('https://www.google.co.jp/search?hl=ja&q='+domain);
17
+ var response = UrlFetchApp.fetch('https://www.google.co.jp/search?hl=ja&q=site:'+domain);
18
18
 
19
19
  var content = response.getContentText();
20
20
 

2

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

2017/02/04 16:03

投稿

hikochang
hikochang

スコア648

test CHANGED
@@ -4,13 +4,27 @@
4
4
 
5
5
  ```code.gs
6
6
 
7
- function getIndexNumber(domain) {
7
+ function test_getDomainIndex() {
8
8
 
9
- var response = UrlFetchApp.fetch('https://www.google.co.jp/search?q=site%3A'+domain);
9
+ getDomainIndex('google.com');
10
10
 
11
- var html = response.getContentText();
11
+ }
12
12
 
13
+
14
+
15
+ function getDomainIndex(domain) {
16
+
17
+ var response = UrlFetchApp.fetch('https://www.google.co.jp/search?hl=ja&q='+domain);
18
+
19
+ var content = response.getContentText();
20
+
21
+ var index = content.
22
+
13
- var index = html.match(/\=\"resultStats\">約\s[0-9\,]+(?=\s件)/)[0].match(/[0-9\,]+/)[0];
23
+ match(/<div class=\"sd\" id=\"resultStats\"[^0-9]*[0-9\,]+/)[0].
24
+
25
+ match(/[0-9\,]+/)[0].
26
+
27
+ replace(/\,/g,"");
14
28
 
15
29
  return index;
16
30
 
@@ -22,10 +36,18 @@
22
36
 
23
37
  spreadsheetでは以下を入力します。
24
38
 
25
- > =getIndexNumber("domain.com")
39
+ > =getDomainIndex("domain.com")
26
40
 
27
41
 
28
42
 
29
43
  実際にはB1セルに以下を入力し、A1セルにドメイン名を入力する感じです。
30
44
 
31
- > =getIndexNumber(A1)
45
+ > =getDomainIndex(A1)
46
+
47
+
48
+
49
+ ---
50
+
51
+ UrlFetchApp.fetchで失敗する件について私自身も興味があり、本質問とは本質的には異なる気がするので、別の質問をしました。
52
+
53
+ [UrlFetchApp.fetchで(エラー: 503)が発生することがある。](https://teratail.com/questions/64619?modal=q-comp)

1

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

2017/02/04 16:00

投稿

hikochang
hikochang

スコア648

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  var html = response.getContentText();
12
12
 
13
- var index = html.match(/\=\"resultStats\">約\s[0-9]+(?=\s件)/)[0].match(/[0-9]+/)[0];
13
+ var index = html.match(/\=\"resultStats\">約\s[0-9\,]+(?=\s件)/)[0].match(/[0-9\,]+/)[0];
14
14
 
15
15
  return index;
16
16