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:

  1. get 35.00 entire text
  2. 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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -