Nokogiri 价格未显示 [英] Nokogiri price doesn't show
问题描述
谁能解释我如何使用 nokogiri 检索价格的值?出现在我创建的刮刀上的值是这个
Can anyone explain how can I retrieve the value of the price using nokogiri? The values that appear on the scraper that I created are the this
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
推荐答案
这是我收到的每个结果:
This is what I'm receiving for each result:
<span class="ProductListElement__price"> </span>
这告诉我,一旦网页加载完毕,JavaScript 就会动态加载价格.
which suggests to me that the prices are dynamically loaded by JavaScript once the webpage has loaded.
为了能够抓取动态加载的数据,您需要使用像 Watir 这样的库Rails 5 支持.
To be able to scrape dynamically-loaded data, you will need to use a library like Watir which is supported by Rails 5.
使用 Watir,您可以等到所有脚本都执行完毕并加载所有数据后,再尝试抓取站点.
With Watir, you are able to wait until all scripts are executed and all data is loaded before attempting to scrape the site.
这篇关于Nokogiri 价格未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!