質問編集履歴

2

コードを追記

2018/01/10 12:50

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -200,4 +200,76 @@
200
200
 
201
201
 
202
202
 
203
- お手数おかけしますが、よろしくお願します。
203
+ 追記:以下にRubyのコードを記載します。
204
+
205
+
206
+
207
+ ```
208
+
209
+ require 'mechanize'
210
+
211
+
212
+
213
+ class Scraping
214
+
215
+ def self.icecream_urls
216
+
217
+ links = [] # 個別ページのリンクを保存する配列
218
+
219
+ agent = Mechanize.new
220
+
221
+ (1..4).each do |i|
222
+
223
+ current_page = agent.get("http://www.morinaga.co.jp/products/list.php?id=030#{i}")
224
+
225
+ elements = current_page.search('.products__list__item a')
226
+
227
+ elements.each do |ele|
228
+
229
+ links << ele.get_attribute('href')
230
+
231
+ end
232
+
233
+ end
234
+
235
+
236
+
237
+
238
+
239
+ links.each do |link| #get_product に引数(個別ページのURL)を渡す
240
+
241
+ get_product('http://www.morinaga.co.jp/products/' + link)
242
+
243
+ end
244
+
245
+ end
246
+
247
+
248
+
249
+ def self.get_product(link) # 個別ページからアイスの情報を取得する
250
+
251
+ agent = Mechanize.new
252
+
253
+ page = agent.get(link)
254
+
255
+ product_name = page.at('.headingType01__txt').inner_text # 商品名
256
+
257
+ image_url = page.at('.products-mainImg img').get_attribute('src') # 商品画像
258
+
259
+
260
+
261
+ serving_size = page.search('dd:nth-child(13)').inner_text # 内容量
262
+
263
+
264
+
265
+ product = Product.where(name: product_name, image_url: image_url, serving_size: serving_size).first_or_initialize
266
+
267
+
268
+
269
+ product.save
270
+
271
+ end
272
+
273
+ end
274
+
275
+ ```

1

コードの追加

2018/01/10 12:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -172,9 +172,23 @@
172
172
 
173
173
  ```
174
174
 
175
- page.at('.products-detailBox__list dt:nth-child(2) dd').inner_text
175
+ serving_size = page.at('.products-detailBox__list dt:nth-child(2) dd').inner_text
176
-
176
+
177
- ```
177
+ ```
178
+
179
+
180
+
181
+ 追記:以下のコードもだめでした。
182
+
183
+
184
+
185
+ ```
186
+
187
+ serving_size = page.at('dl dt:nth-child(2) dd').inner_text
188
+
189
+ ```
190
+
191
+
178
192
 
179
193
 
180
194