Getting price before and after discount separately using nokogiri n Ruby on Rails -
i'm trying learn on scrap these values put in 2 different task:
- get 35.00 entire text
- get 42.00 entire text
below html:
<p style="font-size: 30px; margin-left: -10px; padding: 15px 0pt;"> $35.00 - $42.00 </p>
the code im using entire text below:
node = html_doc.at_css('p') p node.text
you can whole text node.text
, that's far need go nokogiri. there use scan
find numbers , bit of list wrangling (flatten
, map
) , you're done. this:
first, second = node.text.scan(/(\d+(?:\.\d+))/).flatten.map(&:to_f)
that should leave 35.0 in first
, 42.0
in second
. if know numbers prices decimals can simplify regex bit:
first, second = node.text.scan(/(\d+\.\d+)/).flatten.map(&:to_f)
Comments
Post a Comment