質問編集履歴
3
実際のコードを記載した
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,4 +17,99 @@
|
|
17
17
|
function testFunc() {
|
18
18
|
var response = UrlFetchApp.fetch('https://twitter.com/yuzutadashi2/status/1197741551813685251?s=20');
|
19
19
|
}
|
20
|
+
```
|
21
|
+
実際のコード
|
22
|
+
```Google Apps Script
|
23
|
+
// Amazon商品を取得する
|
24
|
+
function getAmazonPrdcts(label, linkUrl, row) {
|
25
|
+
|
26
|
+
// スプレッドシートを取得する
|
27
|
+
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();// Container Bound Script
|
28
|
+
var sheet = spreadsheet.getSheets()[0];// シート数を指定して取得
|
29
|
+
|
30
|
+
// ASINコードを取得する
|
31
|
+
if (linkUrl.match(/dp/.{10}/)) {
|
32
|
+
var asin = linkUrl.match(/dp/.{10}/)[0].replace(/dp/(.{10})/, '$1');
|
33
|
+
} else if (linkUrl.match(/product-reviews/.{10}/)) {
|
34
|
+
asin = linkUrl.match(/product-reviews/.{10}/)[0].replace(/product-reviews/(.{10})/, '$1');
|
35
|
+
}
|
36
|
+
|
37
|
+
// 商品ページを設定する
|
38
|
+
var prdct_URL = 'https://www.amazon.co.jp/dp/'+ asin +'/';
|
39
|
+
|
40
|
+
// Getリクエストのパラメータ
|
41
|
+
var options = {
|
42
|
+
method: "get"
|
43
|
+
};
|
44
|
+
// 商品ページを取得する
|
45
|
+
var reply = url_Request(prdct_URL, options);// 1. URLそのままを取得
|
46
|
+
var response = reply[0];
|
47
|
+
var errorMsg = reply[1];
|
48
|
+
// 商品ページを取得できなかったとき
|
49
|
+
if (response == undefined) {
|
50
|
+
sheet.getRange(row, 5).setValue(errorMsg);// エラーを書き出す
|
51
|
+
return ['', '', ''];
|
52
|
+
}
|
53
|
+
|
54
|
+
// 文字コードを指定して商品ページを取得する
|
55
|
+
var charset = '', src1 = response.getContentText();// 2. 文字列エンコードされた内容を取得
|
56
|
+
if (src1.match(/meta charset="[\s\S]*?"/)) {
|
57
|
+
charset = src1.match(/meta charset="[\s\S]*?"/)[0].replace(/meta charset="([\s\S]*?)"/, '$1')
|
58
|
+
}
|
59
|
+
var src2 = response.getContentText(charset);// 3. 文字コードを指定して内容を取得
|
60
|
+
if (src2.match(/<span id="productTitle" class="a-size-large">[\s\S]*?</span>/)==null) {
|
61
|
+
return ['', '', ''];
|
62
|
+
}
|
63
|
+
var prdctTitle = src2.match(/<span id="productTitle" class="a-size-large">[\s\S]*?</span>/)[0]
|
64
|
+
.replace(/<span id="productTitle" class="a-size-large">([\s\S]*?</span>)/, '$1')
|
65
|
+
.replace(/ /g, '\n').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&') // [HTML特殊文字] 改行, '<', '>', '&' 表示を 文字変換する
|
66
|
+
.replace(/"/g, '"').replace(/'/g, "'") // '"', "'" 表示を 文字変換する
|
67
|
+
.replace(/‒/g, '‒').replace(/–/g, '–').replace(/—/g, '—').replace(/―/g, '―') // '‒', '–', '—', '―' 表示を 文字変換する
|
68
|
+
.replace(/⁓/g, '⁓').replace(/〜/g, '〜').replace(/〰/g, '〰') // '⁓', '〜', '〰' 表示を 文字変換する
|
69
|
+
.replace(/./g, '.');// '.' 表示を 文字変換する
|
70
|
+
|
71
|
+
// メディアアイコンを設定する
|
72
|
+
var mediaIcon = '????';
|
73
|
+
|
74
|
+
// ツイート内のユニコードを分解しないように配列に分ける
|
75
|
+
var shortPrdctTitleArray = prdctTitle.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\s\S]/g) || [];
|
76
|
+
// ツイート44文字(43+'…')に収めて連結する
|
77
|
+
if (shortPrdctTitleArray.length > 44) {
|
78
|
+
shortPrdctTitleArray.splice(43, shortPrdctTitleArray.length-43);// 0開始n番目からn個削除
|
79
|
+
var shortPrdctTitle = mediaIcon + shortPrdctTitleArray.join('').replace(/\s*$/, '') +'…';
|
80
|
+
} else {
|
81
|
+
shortPrdctTitle = mediaIcon + shortPrdctTitleArray.join('');
|
82
|
+
}
|
83
|
+
|
84
|
+
// リンクURLを高評価カスタマーレビューページに設定する
|
85
|
+
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';
|
86
|
+
|
87
|
+
// 商品画像URLを取得する
|
88
|
+
var image_URL = '';
|
89
|
+
if (src2.match(/"mainUrl":".*?"/)) {
|
90
|
+
image_URL = src2.match(/"mainUrl":".*?"/)[0].replace(/"mainUrl":"(.*?)"/, '$1');
|
91
|
+
} else if (src2.match(/data-old-hires=".*?"/)) {
|
92
|
+
image_URL = src2.match(/data-old-hires=".*?"/)[0].replace(/data-old-hires="(.*?)"/, '$1');
|
93
|
+
}
|
94
|
+
|
95
|
+
return [shortPrdctTitle, linkUrl, image_URL];
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
// パラメータを使用してURLを取得する
|
100
|
+
function url_Request(url, options) {
|
101
|
+
// URL取得 or FetchAppエラー10回 したら抜ける
|
102
|
+
for (var i=0; i < 10; i++) {
|
103
|
+
try {// 主処理
|
104
|
+
var response = UrlFetchApp.fetch(url, options);
|
105
|
+
} catch (e) {// エラー処理
|
106
|
+
var errorMsg = e;
|
107
|
+
if (e.toString().match(/エラー: 404/)) { break; }
|
108
|
+
Utilities.sleep(7 * 1000);// 7秒
|
109
|
+
}
|
110
|
+
if (response != undefined) { break; }
|
111
|
+
}
|
112
|
+
// URLを取得できなかったら undefined が返る
|
113
|
+
return [response, errorMsg];
|
114
|
+
}
|
20
115
|
```
|
2
応答を翻訳した
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
> https://www.amazon.co.jp のリクエストに失敗しました(エラー: 503)。サーバー応答の一部: <html> <head> <META HTTP-EQUIV="Content-Type" content="text/html; charset=Shift_JIS"> <title> ²Àf𨩯µÄ¢Ü·I </title> <style type="tex...(応答の全文を見るには muteHttpExceptions オプションを使用してください)(行 2、ファイル「コード」)
|
3
3
|
|
4
4
|
|
5
|
-
応答の全文
|
6
|
-
>
|
7
|
-
|
5
|
+
応答の全文中のコメント
|
6
|
+
> Amazonデータへの自動アクセスについては、api-services-support @ amazon.comにお問い合わせください。
|
7
|
+
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はユースケースの広告用です。
|
8
8
|
|
9
9
|
当該エラー発生コード
|
10
10
|
```Google Apps Script
|
1
タイトルの変更と現在の応答の追記
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
[Google Apps Script] UrlFetchApp の
|
1
|
+
[Google Apps Script] UrlFetchApp の 正しいレスポンスを得るには?
|
body
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
エラーメッセージ
|
2
2
|
> https://www.amazon.co.jp のリクエストに失敗しました(エラー: 503)。サーバー応答の一部: <html> <head> <META HTTP-EQUIV="Content-Type" content="text/html; charset=Shift_JIS"> <title> ²Àf𨩯µÄ¢Ü·I </title> <style type="tex...(応答の全文を見るには muteHttpExceptions オプションを使用してください)(行 2、ファイル「コード」)
|
3
3
|
|
4
|
-
|
4
|
+
|
5
|
+
応答の全文の中のコメント
|
6
|
+
> To discuss automated access to Amazon data please contact api-services-support@amazon.com.
|
7
|
+
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.
|
8
|
+
|
9
|
+
当該エラー発生コード
|
5
10
|
```Google Apps Script
|
6
11
|
function testFunc() {
|
7
12
|
var response = UrlFetchApp.fetch('https://www.amazon.co.jp/dp/4800288487/');
|