DEV Community

Cover image for How to count html tags in a web page by using web url
Muhammad Ahmad
Muhammad Ahmad

Posted on

How to count html tags in a web page by using web url

  • Create new rails app

  • Install following Gems

gem 'nokogiri' gem 'open-uri' 
Enter fullscreen mode Exit fullscreen mode
  • Script
begin @doc = Nokogiri::HTML(URI.open(params[:page][:url]).read) @summary = @doc.xpath("//*").map(&:name).each_with_object({}) {|n, r| r[n] = (r[n] || 0) + 1 } rescue Exception => e puts "Couldn't read \"#{ params[:page][:url] }\": #{ e }" end 
Enter fullscreen mode Exit fullscreen mode
  • View
<% @summary.each do |tag, count| %> <div> <%= tag %>: <%= count %> </div> <% end %> 
Enter fullscreen mode Exit fullscreen mode

Github Repo: rails-UrltoHtml
Demo Link: rails-UrltoHtml

Top comments (0)