質問編集履歴

2

文章の改善

2016/11/24 20:36

投稿

snoooopy
snoooopy

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,35 +1,299 @@
1
1
  現在、Rubyを用いてAmazonサイトから書籍のタイトル、画像のクローリングを行っています。
2
2
 
3
-
4
-
5
-
6
-
7
3
  そこで、
8
4
 
9
-
10
-
11
5
  [Amazon.co.jp: 文芸作品 - 文学・評論: 本](https://www.amazon.co.jp/s/ref=lp_466284_nr_n_0?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A548206&bbn=466284&ie=UTF8&qid=1479948677&rnid=466284)
12
6
 
13
-
14
-
15
7
  上記のページ内の作品名は抽出することは出来ました。
16
8
 
17
-
18
-
19
9
  しかし、次のページへクローリングさせる方法がわかりません。
20
10
 
21
11
 
22
12
 
23
13
  Rubyのライブラリでnokogiriとanemoneを使って、
24
14
 
25
- 巡回する階層値と絞り込みのon_page_likeメソッドを用いて調整していましたが、上手巡回していせんでした。
26
-
27
-
28
-
29
- 最終的は50ページまでクローリングさせたいと考えています。
30
-
31
-
32
-
33
- サンプルプログラム,解決策をご教授いただけますと幸いです。
15
+ プログラムとしては階層値に1をいれanemone.focus_crawlメソッドを用いて2ページ目だけに行ように絞り込みました。
16
+
17
+
18
+
19
+ ```ここ言語を入力
20
+
21
+ require 'anemone'
22
+
23
+ require 'nokogiri'
24
+
25
+ require 'kconv'
26
+
27
+
28
+
29
+ #def save_image(url)
30
+
31
+
32
+
33
+ opts = {
34
+
35
+
36
+
37
+ :proxy_host => 'proxy.ksc.kwansei.ac.jp',
38
+
39
+ :proxy_port => '8080',
40
+
41
+ depth_limit:1
42
+
43
+ }
44
+
45
+
46
+
47
+ urls = []
48
+
49
+ #文学評論
50
+
51
+
52
+
53
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_0?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A548206&bbn=466284&ie=UTF8&qid=1479178349&rnid=466284")
54
+
55
+ #文芸作品
56
+
57
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_1?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507156&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
58
+
59
+ #歴史時代小説
60
+
61
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_2?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507214&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
62
+
63
+ #経済社会小説
64
+
65
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_3?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507216&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
66
+
67
+ #ミステリー
68
+
69
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_4?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507300&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
70
+
71
+ #SF
72
+
73
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_5?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A507072&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
74
+
75
+ #ロマンス
76
+
77
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_6?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467258&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
78
+
79
+ #エッセー
80
+
81
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_7?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467260&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
82
+
83
+ #古典
84
+
85
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_8?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467256&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
86
+
87
+ #詩歌
88
+
89
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_9?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467266&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
90
+
91
+ #戯曲
92
+
93
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_10?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467262&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
94
+
95
+ #評論
96
+
97
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_11?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467264&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
98
+
99
+ #伝承
100
+
101
+ urls.push("https://www.amazon.co.jp/s/ref=lp_466284_nr_n_12?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A467252&bbn=466284&ie=UTF8&qid=1479970894&rnid=466284")
102
+
103
+ #全集
104
+
105
+
106
+
107
+
108
+
109
+ Anemone.crawl(urls,opts)do|anemone|
110
+
111
+
112
+
113
+ #巡回先の絞り込み
114
+
115
+ #ページごとにどのリンク先を巡回するか指定する
116
+
117
+
118
+
119
+ PATTERN = [%Q[//*[@id="pagnNextLink"]]]
120
+
121
+
122
+
123
+ anemone.focus_crawl do |page|
124
+
125
+ page.links.keep_if { |link|
126
+
127
+ #link.to_s.match(PATTERN)
128
+
129
+ }
130
+
131
+ end
132
+
133
+
134
+
135
+
136
+
137
+ # 正規表現で一致したページのみ処理
138
+
139
+
140
+
141
+ #PATTERN = %r[466298\/+|466282\/+|2291657051\/+|2291905051\/+]
142
+
143
+
144
+
145
+ #anemone.on_pages_like(PATTERN) do |page|
146
+
147
+
148
+
149
+ #puts page.url
150
+
151
+
152
+
153
+ #end
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ anemone.on_every_page do |page|
162
+
163
+
164
+
165
+ doc = Nokogiri::HTML.parse(page.body.toutf8)
166
+
167
+
168
+
169
+ bodys = doc.xpath(%Q[//*[@id="result_0"]/div/div[3]/div[1]/a/h2])
170
+
171
+ bodys1 = doc.xpath(%Q[//*[@id="result_1"]/div/div[3]/div[1]/a/h2])
172
+
173
+ bodys2 = doc.xpath(%Q[//*[@id="result_2"]/div/div[3]/div[1]/a/h2])
174
+
175
+ bodys3 = doc.xpath(%Q[//*[@id="result_3"]/div/div[3]/div[1]/a/h2])
176
+
177
+ bodys4 = doc.xpath(%Q[//*[@id="result_4"]/div/div[3]/div[1]/a/h2])
178
+
179
+ bodys5 = doc.xpath(%Q[//*[@id="result_5"]/div/div[3]/div[1]/a/h2])
180
+
181
+ bodys6 = doc.xpath(%Q[//*[@id="result_6"]/div/div[3]/div[1]/a/h2])
182
+
183
+ bodys7 = doc.xpath(%Q[//*[@id="result_7"]/div/div[3]/div[1]/a/h2])
184
+
185
+ bodys8 = doc.xpath(%Q[//*[@id="result_8"]/div/div[3]/div[1]/a/h2])
186
+
187
+ bodys9 = doc.xpath(%Q[//*[@id="result_9"]/div/div[3]/div[1]/a/h2])
188
+
189
+ bodys10 = doc.xpath(%Q[//*[@id="result_10"]/div/div[3]/div[1]/a/h2])
190
+
191
+ bodys11 = doc.xpath(%Q[//*[@id="result_11"]/div/div[3]/div[1]/a/h2])
192
+
193
+ bodys12 = doc.xpath(%Q[//*[@id="result_12"]/div/div[3]/div[1]/a/h2])
194
+
195
+ bodys13 = doc.xpath(%Q[//*[@id="result_13"]/div/div[3]/div[1]/a/h2])
196
+
197
+ bodys14 = doc.xpath(%Q[//*[@id="result_14"]/div/div[3]/div[1]/a/h2])
198
+
199
+ bodys15 = doc.xpath(%Q[//*[@id="result_15"]/div/div[3]/div[1]/a/h2])
200
+
201
+ bodys16 = doc.xpath(%Q[//*[@id="result_16"]/div/div[3]/div[1]/a/h2])
202
+
203
+ bodys17 = doc.xpath(%Q[//*[@id="result_17"]/div/div[3]/div[1]/a/h2])
204
+
205
+ bodys18 = doc.xpath(%Q[//*[@id="result_18"]/div/div[3]/div[1]/a/h2])
206
+
207
+ bodys19 = doc.xpath(%Q[//*[@id="result_19"]/div/div[3]/div[1]/a/h2])
208
+
209
+ bodys20 = doc.xpath(%Q[//*[@id="result_20"]/div/div[3]/div[1]/a/h2])
210
+
211
+ bodys21 = doc.xpath(%Q[//*[@id="result_21"]/div/div[3]/div[1]/a/h2])
212
+
213
+ bodys22 = doc.xpath(%Q[//*[@id="result_22"]/div/div[3]/div[1]/a/h2])
214
+
215
+ bodys23 = doc.xpath(%Q[//*[@id="result_23"]/div/div[3]/div[1]/a/h2])
216
+
217
+ bodys24 = doc.xpath(%Q[//*[@id="result_24"]/div/div[3]/div[1]/a/h2])
218
+
219
+ bodys25 = doc.xpath(%Q[//*[@id="result_25"]/div/div[3]/div[1]/a/h2])
220
+
221
+
222
+
223
+ puts bodys.text + "a"
224
+
225
+ puts bodys1.text + "abc"
226
+
227
+ puts bodys2.text + "a"
228
+
229
+ puts bodys3.text + "a"
230
+
231
+ puts bodys4.text + "a"
232
+
233
+ puts bodys5.text + "a"
234
+
235
+ puts bodys6.text + "abcdef"
236
+
237
+ puts bodys7.text + "a"
238
+
239
+ puts bodys8.text + "a"
240
+
241
+ puts bodys9.text + "a"
242
+
243
+ puts bodys10.text + "a"
244
+
245
+ puts bodys11.text + "a"
246
+
247
+ puts bodys12.text + "a"
248
+
249
+ puts bodys13.text + "a"
250
+
251
+ puts bodys14.text + "abcdefgh"
252
+
253
+ puts bodys15.text + "a"
254
+
255
+ puts bodys16.text + "a"
256
+
257
+ puts bodys17.text + "a"
258
+
259
+ puts bodys18.text + "a"
260
+
261
+ puts bodys19.text + "a"
262
+
263
+ puts bodys20.text + "abcdefghijk"
264
+
265
+ puts bodys21.text + "a"
266
+
267
+ puts bodys22.text + "a"
268
+
269
+ puts bodys23.text + "a"
270
+
271
+ puts bodys24.text + "a"
272
+
273
+ puts bodys25.text + "a"
274
+
275
+ puts " "
276
+
277
+
278
+
279
+ end
280
+
281
+
282
+
283
+ end
284
+
285
+
286
+
287
+ ```
288
+
289
+ ページ移動の確認を行うために、二ページ目の最初の二つの書籍のタイトルを取ってくるようにしたんですが、どうしても表示されません。
290
+
291
+
292
+
293
+ 最終的には前ページのクローリングをさせたいと考えています。
294
+
295
+
296
+
297
+ 解決策をご教授いただけますと幸いです。
34
298
 
35
299
  どうぞ宜しくお願いいたします。

1

文章の改善

2016/11/24 20:36

投稿

snoooopy
snoooopy

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,16 +1,22 @@
1
1
  現在、Rubyを用いてAmazonサイトから書籍のタイトル、画像のクローリングを行っています。
2
-
3
- そこで、例えば
4
2
 
5
3
 
6
4
 
5
+
6
+
7
- https://www.amazon.co.jp/s/ref=lp_466284_nr_n_0?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A548206&bbn=466284&ie=UTF8&qid=1479948677&rnid=466284
7
+ そこで、
8
8
 
9
9
 
10
10
 
11
- 上記のページから次のページへクローリングさせる方法がわかりません。
11
+ [Amazon.co.jp: 文芸作品 - 文学・評論: 本](https://www.amazon.co.jp/s/ref=lp_466284_nr_n_0?fst=as%3Aoff&rh=n%3A465392%2Cn%3A%21465610%2Cn%3A466284%2Cn%3A548206&bbn=466284&ie=UTF8&qid=1479948677&rnid=466284)
12
12
 
13
+
14
+
15
+ 上記のページ内の作品名は抽出することは出来ました。
16
+
17
+
18
+
13
- 最終的には50ページまでクローリングさせたいと考えてい
19
+ しかし、次のページクローリングさせる方法がわかりせん
14
20
 
15
21
 
16
22
 
@@ -20,7 +26,7 @@
20
26
 
21
27
 
22
28
 
23
-
29
+ 最終的には50ページまでクローリングさせたいと考えています。
24
30
 
25
31
 
26
32