shamshul2007 shamshul2007 - 5 months ago 9
Ruby Question

How do I scrape HTML using Nokogiri?

I am not able to scrap the price of the product and the output which I get is as follow for each price:

<div class="pu-final">
<span class="fk-font-17 fk-bold">Rs. 1999</span>
</div>


My code is:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "http://www.flipkart.com/mens-footwear/shoes/casual-shoes/pr?sid=osp,cil,nit,e1f"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".gu4,.browse-product").each do |item|
title = item.at_css(".fk-display-block,.title").text
puts title
puts "================="
price = item.at_css(".pu-final")
puts price
end

Answer

I tried the same code with a small change and it worked fine. Give it a go.

change

price = item.at_css(".pu-final")

to

price = item.at_css(".pu-final").text unless item.at_css(".pu-final").nil?