Fetch Content From HTML in Ruby

Categories Ruby On Rails
1

The best way to fetch data from html based on css or html tags is to use NOKOGIRI gem.

Here is the process to achieve it:

$ sudo gem install nokogiri

# Open your controller

#Include open-uri & nokogiri

require ‘open-uri’

require ‘nokogiri’

class FetchController < ApplicationController

def index

doc = Nokogiri::HTML(open(‘kunalinfotech.net/company/about-us.html’))

doc.css(‘body,h1′).each do |link| @fetch = link.content

end

# Index.html.erb

<%= @fetch %>

 

One thought on “Fetch Content From HTML in Ruby

Leave a Reply

Your email address will not be published. Required fields are marked *

*


+ seven = 12

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>