任何人都可以解释如何使用nokogiri检索价格值吗? 我创建的刮板上显示的值是
Costa Rica
<span class="ProductListElement__price"> </span>
India
<span class="ProductListElement__price"> </span>
Indonesia
<span class="ProductListElement__price"> </span>
Colombia
<span class="ProductListElement__price"> </span>
Nicaragua
<span class="ProductListElement__price"> </span>
Ethiopia
<span class="ProductListElement__price"> </span>
Master Origin Pack (50 cápsulas)
<span class="ProductListElement__price"> </span>
CAFÉ İSTANBUL
<span class="ProductListElement__price"> </span>
Envivo Lungo
<span class="ProductListElement__price"> </span>
Fortissio Lungo
<span class="ProductListElement__price"> </span>
Vivalto Lungo
<span class="ProductListElement__price"> </span>
Linizio Lungo
<span class="ProductListElement__price"> </span>
Livanto
<span class="ProductListElement__price"> </span>
Capriccio
<span class="ProductListElement__price"> </span>
Volluto
<span class="ProductListElement__price"> </span>
Cosi
<span class="ProductListElement__price"> </span>
Kazaar
<span class="ProductListElement__price"> </span>
Dharkan
<span class="ProductListElement__price"> </span>
Ristretto
<span class="ProductListElement__price"> </span>
Arpeggio
<span class="ProductListElement__price"> </span>
Roma
<span class="ProductListElement__price"> </span>
Ristretto Decaffeinato
<span class="ProductListElement__price"> </span>
Arpeggio Decaffeinato
<span class="ProductListElement__price"> </span>
Volluto Decaffeinato
<span class="ProductListElement__price"> </span>
Vivalto Lungo Decaffeinato
<span class="ProductListElement__price"> </span>
Vanilio
<span class="ProductListElement__price"> </span>
Caramelito
<span class="ProductListElement__price"> </span>
我的控制器是这样:
class CupsController < ApplicationController
class Entry
def initialize(name, price)
@name = name
@price = price
end
attr_reader :name
attr_reader :price
end
def cups
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('https://www.nespresso.com/pt/pt/order/capsules'))
entries = doc.css("article.ProductListElement")
@entriesArray = []
entries.each do |entry|
name = entry.css('.ProductListElement__name').text
price = entry.css('span.ProductListElement__price')
@entriesArray << Entry.new(name, price)
@name = name
@price = price
end
render template: 'cups/home'
end
end
答案 0 :(得分:0)
这是我收到的每个结果的结果:
<span class="ProductListElement__price"> </span>
这向我建议,一旦网页加载,价格就会由JavaScript动态加载。
要能够抓取动态加载的数据,您将需要使用Rails 5支持的Watir之类的库。
使用Watir,您可以等到执行所有脚本并加载所有数据之后再尝试刮取网站。