質問編集履歴

2

コードの全容を追記しました

2017/03/12 07:04

投稿

SASAKI_APPL
SASAKI_APPL

スコア100

test CHANGED
File without changes
test CHANGED
@@ -26,8 +26,6 @@
26
26
 
27
27
  var core = UrlFetchApp.fetch("https://catalog.library.metro.tokyo.jp/winj/opac/search-detail.do?org.apache.struts.taglib.html.TOKEN=1d95d116da40c48b0a1b726593e72d0c&cmb_column1=title&txt_word1=&cmb_like1=2&cmb_unit1=0&cmb_column2=author&txt_word2=&cmb_like2=2&cmb_unit2=0&cmb_column3=publisher&txt_word3=&cmb_like3=2&cmb_unit3=0&cmb_column4=fulltext&txt_word4=&cmb_like4=2&cmb_unit4=0&txt_atykey1=&chk_atytp1=11+21&hid_aty_column1=oatyid_11_21&txt_atyword1_dummy=&cmb_unit_aty=0&txt_atykey2=&chk_atytp2=12+22&hid_aty_column2=oatyid_12_22&txt_atyword2_dummy=&txt_ndc=&txt_stpubdate=&txt_edpubdate=&cmb_form=&cmb_littp=&cmb_lang=&txt_holcd=5019490166&cmb_code_column=isbn&txt_code=&txt_magcd=&txt_lom=&txt_cln1=&txt_cln2=&txt_cln3=&cmb_volume_column=volume&txt_stvolume=&txt_edvolume=&chk_catph=11+31&chk_catph=12+22+32&chk_catph=13+33&chk_area=01&chk_hol1tp=101+201+211+221+231+241+251+301+401+411+501+601+801&chk_hol1tp=103+203+213+223+233+243+253+303+403+413+503+603+803&chk_hol1tp=104+204+214+224+234+244+254+304+404+414+504+604+804&chk_hol1tp=105+205+215+225+235+245+255+305+405+415+505+605+805&chk_hol1tp=106+206+216+226+236+246+256+306+406+416+506+606+806&chk_hol1tp=107+207+217+227+237+247+257+307+407+417+507+607+707+807&chk_hol1tp=221+223+224+225+226+227+251+253+254+255+256+257&chk_hol1tp=201+203+204+205+206+207+231+233+234+235+236+237&chk_hol1tp=211+213+214+215+216+217+241+243+244+245+246+247&cmb_column9=ctntp&cmb_like9=0&cmb_unit9=0&txt_word9=&cmb_order=pubYear&opt_order=1&opt_pagesize=50&submit_btn_searchDetailSelAr=検索",options);
28
28
 
29
-
30
-
31
29
  var response = UrlFetchApp.fetch("https://catalog.library.metro.tokyo.jp/winj/opac/search-detail.do?org.apache.struts.taglib.html.TOKEN=1d95d116da40c48b0a1b726593e72d0c&cmb_column1=title&txt_word1=&cmb_like1=2&cmb_unit1=0&cmb_column2=author&txt_word2=&cmb_like2=2&cmb_unit2=0&cmb_column3=publisher&txt_word3=&cmb_like3=2&cmb_unit3=0&cmb_column4=fulltext&txt_word4=&cmb_like4=2&cmb_unit4=0&txt_atykey1=&chk_atytp1=11+21&hid_aty_column1=oatyid_11_21&txt_atyword1_dummy=&cmb_unit_aty=0&txt_atykey2=&chk_atytp2=12+22&hid_aty_column2=oatyid_12_22&txt_atyword2_dummy=&txt_ndc=&txt_stpubdate=&txt_edpubdate=&cmb_form=&cmb_littp=&cmb_lang=&txt_holcd=5019490166&cmb_code_column=isbn&txt_code=&txt_magcd=&txt_lom=&txt_cln1=&txt_cln2=&txt_cln3=&cmb_volume_column=volume&txt_stvolume=&txt_edvolume=&chk_catph=11+31&chk_catph=12+22+32&chk_catph=13+33&chk_area=01&chk_hol1tp=101+201+211+221+231+241+251+301+401+411+501+601+801&chk_hol1tp=103+203+213+223+233+243+253+303+403+413+503+603+803&chk_hol1tp=104+204+214+224+234+244+254+304+404+414+504+604+804&chk_hol1tp=105+205+215+225+235+245+255+305+405+415+505+605+805&chk_hol1tp=106+206+216+226+236+246+256+306+406+416+506+606+806&chk_hol1tp=107+207+217+227+237+247+257+307+407+417+507+607+707+807&chk_hol1tp=221+223+224+225+226+227+251+253+254+255+256+257&chk_hol1tp=201+203+204+205+206+207+231+233+234+235+236+237&chk_hol1tp=211+213+214+215+216+217+241+243+244+245+246+247&cmb_column9=ctntp&cmb_like9=0&cmb_unit9=0&txt_word9=&cmb_order=pubYear&opt_order=1&opt_pagesize=50&submit_btn_searchDetailSelAr=検索",options);
32
30
 
33
31
  Logger.log(response);
@@ -37,3 +35,87 @@
37
35
 
38
36
 
39
37
  **どのようにしたら、検索結果のページをUrlFetchでアクセスできるのでしょうか。**
38
+
39
+
40
+
41
+ *3/12追記
42
+
43
+ Cookie取得まではできたのですが、以下のようにしても結果が変わりません。
44
+
45
+ 何かミスなどがあったらご教授ください。
46
+
47
+ ```JavaScript
48
+
49
+ function metro(){
50
+
51
+
52
+
53
+ var url = "https://catalog.library.metro.tokyo.jp/winj/opac/search-detail.do?org.apache.struts.taglib.html.TOKEN=1d95d116da40c48b0a1b726593e72d0c&cmb_column1=title&txt_word1=&cmb_like1=2&cmb_unit1=0&cmb_column2=author&txt_word2=&cmb_like2=2&cmb_unit2=0&cmb_column3=publisher&txt_word3=&cmb_like3=2&cmb_unit3=0&cmb_column4=fulltext&txt_word4=&cmb_like4=2&cmb_unit4=0&txt_atykey1=&chk_atytp1=11+21&hid_aty_column1=oatyid_11_21&txt_atyword1_dummy=&cmb_unit_aty=0&txt_atykey2=&chk_atytp2=12+22&hid_aty_column2=oatyid_12_22&txt_atyword2_dummy=&txt_ndc=&txt_stpubdate=&txt_edpubdate=&cmb_form=&cmb_littp=&cmb_lang=&txt_holcd=5019490166&cmb_code_column=isbn&txt_code=&txt_magcd=&txt_lom=&txt_cln1=&txt_cln2=&txt_cln3=&cmb_volume_column=volume&txt_stvolume=&txt_edvolume=&chk_catph=11+31&chk_catph=12+22+32&chk_catph=13+33&chk_area=01&chk_hol1tp=101+201+211+221+231+241+251+301+401+411+501+601+801&chk_hol1tp=103+203+213+223+233+243+253+303+403+413+503+603+803&chk_hol1tp=104+204+214+224+234+244+254+304+404+414+504+604+804&chk_hol1tp=105+205+215+225+235+245+255+305+405+415+505+605+805&chk_hol1tp=106+206+216+226+236+246+256+306+406+416+506+606+806&chk_hol1tp=107+207+217+227+237+247+257+307+407+417+507+607+707+807&chk_hol1tp=221+223+224+225+226+227+251+253+254+255+256+257&chk_hol1tp=201+203+204+205+206+207+231+233+234+235+236+237&chk_hol1tp=211+213+214+215+216+217+241+243+244+245+246+247&cmb_column9=ctntp&cmb_like9=0&cmb_unit9=0&txt_word9=&cmb_order=pubYear&opt_order=1&opt_pagesize=50&submit_btn_searchDetailSelAr=検索";
54
+
55
+ var options = {
56
+
57
+ "method" : "post",
58
+
59
+ followRedirects: false
60
+
61
+ }
62
+
63
+
64
+
65
+ var core = UrlFetchApp.fetch(url,options);
66
+
67
+ Logger.log("core:"+core)
68
+
69
+
70
+
71
+ var headers = core.getAllHeaders();
72
+
73
+ var cookies = [];
74
+
75
+
76
+
77
+ if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
78
+
79
+ // Set-Cookieヘッダーが2つ以上の場合はheaders['Set-Cookie']の中身は配列
80
+
81
+ var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
82
+
83
+ for (var i = 0; i < cookies.length; i++) {
84
+
85
+ // Set-Cookieヘッダーからname=valueだけ取り出し、セミコロン以降の属性は除外する
86
+
87
+ cookies[i] = cookies[i].split( ';' )[0];
88
+
89
+ };
90
+
91
+ }
92
+
93
+ Logger.log(cookies);
94
+
95
+
96
+
97
+ var headers = {
98
+
99
+ "Cookie" : cookies
100
+
101
+ }
102
+
103
+ // リクエストオプションにheadersを設定
104
+
105
+ var options = {
106
+
107
+ "method" : "GET",
108
+
109
+ muteHttpExceptions:true,
110
+
111
+ "headers" : headers
112
+
113
+ }
114
+
115
+ var response = UrlFetchApp.fetch(url,options);
116
+
117
+ Logger.log("response:"+response);
118
+
119
+ }
120
+
121
+ ```

1

タイトルを修正しました

2017/03/12 07:04

投稿

SASAKI_APPL
SASAKI_APPL

スコア100

test CHANGED
@@ -1 +1 @@
1
- UrlFetchApp
1
+ Cookieが必要なサイトをUrlFetchAppでスクレイピングする方法
test CHANGED
File without changes