shamshul2007 shamshul2007 - 1 year ago 101
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>

My code is:

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

url = ",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

Answer Source

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


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


price = item.at_css(".pu-final").text unless item.at_css(".pu-final").nil?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download