質問編集履歴
3
実際のコードを記載した
test
CHANGED
File without changes
|
test
CHANGED
@@ -37,3 +37,193 @@
|
|
37
37
|
}
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
実際のコード
|
42
|
+
|
43
|
+
```Google Apps Script
|
44
|
+
|
45
|
+
// Amazon商品を取得する
|
46
|
+
|
47
|
+
function getAmazonPrdcts(label, linkUrl, row) {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
// スプレッドシートを取得する
|
52
|
+
|
53
|
+
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();// Container Bound Script
|
54
|
+
|
55
|
+
var sheet = spreadsheet.getSheets()[0];// シート数を指定して取得
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
// ASINコードを取得する
|
60
|
+
|
61
|
+
if (linkUrl.match(/dp/.{10}/)) {
|
62
|
+
|
63
|
+
var asin = linkUrl.match(/dp/.{10}/)[0].replace(/dp/(.{10})/, '$1');
|
64
|
+
|
65
|
+
} else if (linkUrl.match(/product-reviews/.{10}/)) {
|
66
|
+
|
67
|
+
asin = linkUrl.match(/product-reviews/.{10}/)[0].replace(/product-reviews/(.{10})/, '$1');
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// 商品ページを設定する
|
74
|
+
|
75
|
+
var prdct_URL = 'https://www.amazon.co.jp/dp/'+ asin +'/';
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
// Getリクエストのパラメータ
|
80
|
+
|
81
|
+
var options = {
|
82
|
+
|
83
|
+
method: "get"
|
84
|
+
|
85
|
+
};
|
86
|
+
|
87
|
+
// 商品ページを取得する
|
88
|
+
|
89
|
+
var reply = url_Request(prdct_URL, options);// 1. URLそのままを取得
|
90
|
+
|
91
|
+
var response = reply[0];
|
92
|
+
|
93
|
+
var errorMsg = reply[1];
|
94
|
+
|
95
|
+
// 商品ページを取得できなかったとき
|
96
|
+
|
97
|
+
if (response == undefined) {
|
98
|
+
|
99
|
+
sheet.getRange(row, 5).setValue(errorMsg);// エラーを書き出す
|
100
|
+
|
101
|
+
return ['', '', ''];
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
// 文字コードを指定して商品ページを取得する
|
108
|
+
|
109
|
+
var charset = '', src1 = response.getContentText();// 2. 文字列エンコードされた内容を取得
|
110
|
+
|
111
|
+
if (src1.match(/meta charset="[\s\S]*?"/)) {
|
112
|
+
|
113
|
+
charset = src1.match(/meta charset="[\s\S]*?"/)[0].replace(/meta charset="([\s\S]*?)"/, '$1')
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
var src2 = response.getContentText(charset);// 3. 文字コードを指定して内容を取得
|
118
|
+
|
119
|
+
if (src2.match(/<span id="productTitle" class="a-size-large">[\s\S]*?</span>/)==null) {
|
120
|
+
|
121
|
+
return ['', '', ''];
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
var prdctTitle = src2.match(/<span id="productTitle" class="a-size-large">[\s\S]*?</span>/)[0]
|
126
|
+
|
127
|
+
.replace(/<span id="productTitle" class="a-size-large">([\s\S]*?</span>)/, '$1')
|
128
|
+
|
129
|
+
.replace(/ /g, '\n').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&') // [HTML特殊文字] 改行, '<', '>', '&' 表示を 文字変換する
|
130
|
+
|
131
|
+
.replace(/"/g, '"').replace(/'/g, "'") // '"', "'" 表示を 文字変換する
|
132
|
+
|
133
|
+
.replace(/‒/g, '‒').replace(/–/g, '–').replace(/—/g, '—').replace(/―/g, '―') // '‒', '–', '—', '―' 表示を 文字変換する
|
134
|
+
|
135
|
+
.replace(/⁓/g, '⁓').replace(/〜/g, '〜').replace(/〰/g, '〰') // '⁓', '〜', '〰' 表示を 文字変換する
|
136
|
+
|
137
|
+
.replace(/./g, '.');// '.' 表示を 文字変換する
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
// メディアアイコンを設定する
|
142
|
+
|
143
|
+
var mediaIcon = '????';
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// ツイート内のユニコードを分解しないように配列に分ける
|
148
|
+
|
149
|
+
var shortPrdctTitleArray = prdctTitle.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\s\S]/g) || [];
|
150
|
+
|
151
|
+
// ツイート44文字(43+'…')に収めて連結する
|
152
|
+
|
153
|
+
if (shortPrdctTitleArray.length > 44) {
|
154
|
+
|
155
|
+
shortPrdctTitleArray.splice(43, shortPrdctTitleArray.length-43);// 0開始n番目からn個削除
|
156
|
+
|
157
|
+
var shortPrdctTitle = mediaIcon + shortPrdctTitleArray.join('').replace(/\s*$/, '') +'…';
|
158
|
+
|
159
|
+
} else {
|
160
|
+
|
161
|
+
shortPrdctTitle = mediaIcon + shortPrdctTitleArray.join('');
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
// リンクURLを高評価カスタマーレビューページに設定する
|
168
|
+
|
169
|
+
linkUrl = 'https://www.amazon.co.jp/product-reviews/'+ asin +'/ref=cm_cr_arp_d_viewopt_sr?ie=UTF8&filterByStar=positive&reviewerType=all_reviews&pageNumber=1';
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
// 商品画像URLを取得する
|
174
|
+
|
175
|
+
var image_URL = '';
|
176
|
+
|
177
|
+
if (src2.match(/"mainUrl":".*?"/)) {
|
178
|
+
|
179
|
+
image_URL = src2.match(/"mainUrl":".*?"/)[0].replace(/"mainUrl":"(.*?)"/, '$1');
|
180
|
+
|
181
|
+
} else if (src2.match(/data-old-hires=".*?"/)) {
|
182
|
+
|
183
|
+
image_URL = src2.match(/data-old-hires=".*?"/)[0].replace(/data-old-hires="(.*?)"/, '$1');
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
return [shortPrdctTitle, linkUrl, image_URL];
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
// パラメータを使用してURLを取得する
|
198
|
+
|
199
|
+
function url_Request(url, options) {
|
200
|
+
|
201
|
+
// URL取得 or FetchAppエラー10回 したら抜ける
|
202
|
+
|
203
|
+
for (var i=0; i < 10; i++) {
|
204
|
+
|
205
|
+
try {// 主処理
|
206
|
+
|
207
|
+
var response = UrlFetchApp.fetch(url, options);
|
208
|
+
|
209
|
+
} catch (e) {// エラー処理
|
210
|
+
|
211
|
+
var errorMsg = e;
|
212
|
+
|
213
|
+
if (e.toString().match(/エラー: 404/)) { break; }
|
214
|
+
|
215
|
+
Utilities.sleep(7 * 1000);// 7秒
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
if (response != undefined) { break; }
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
// URLを取得できなかったら undefined が返る
|
224
|
+
|
225
|
+
return [response, errorMsg];
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
```
|
2
応答を翻訳した
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,11 +6,11 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
応答の全文
|
9
|
+
応答の全文中のコメント
|
10
10
|
|
11
|
-
>
|
11
|
+
> Amazonデータへの自動アクセスについては、api-services-support @ amazon.comにお問い合わせください。
|
12
12
|
|
13
|
-
|
13
|
+
APIへの移行については、https://developer.amazonservices.jp/ref=rm_5_svのMarketplace API、またはhttps://affiliate.amazon.co.jp/gp/advertising/apiのProduct Advertising APIを参照してください。 /detail/main.html/ref=rm_5_acはユースケースの広告用です。
|
14
14
|
|
15
15
|
|
16
16
|
|
1
タイトルの変更と現在の応答の追記
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
[Google Apps Script] UrlFetchApp の
|
1
|
+
[Google Apps Script] UrlFetchApp の 正しいレスポンスを得るには?
|
test
CHANGED
@@ -4,7 +4,17 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
|
7
|
+
|
8
|
+
|
9
|
+
応答の全文の中のコメント
|
10
|
+
|
11
|
+
> To discuss automated access to Amazon data please contact api-services-support@amazon.com.
|
12
|
+
|
13
|
+
For information about migrating to our APIs refer to our Marketplace APIs at https://developer.amazonservices.jp/ref=rm_5_sv, or our Product Advertising API at https://affiliate.amazon.co.jp/gp/advertising/api/detail/main.html/ref=rm_5_ac for advertising use cases.
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
当該エラー発生コード
|
8
18
|
|
9
19
|
```Google Apps Script
|
10
20
|
|