Search code examples
ruby-on-railsrubyruby-on-rails-5nokogiriopen-uri

Rails won't read a link with nokogiri and open-uri


I have a controller which gets a url passed as a parameter and I am trying to scrape the entire page at that url. But when I try to read the url I get the following error: No such file or directory @ rb_sysopen - www.google.com

Controller:

lass PageScraperController < ApplicationController
    require 'nokogiri'
    require 'open-uri'
    require 'diffy'
    require 'htmlentities'

    def scrape
    require 'open-uri'
        @url = watched_link_params.to_s
        @url = @url.slice([email protected])
        puts "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG#{@url}"

        page = Nokogiri::HTML(open(@url))
        # coder = HTMLEntities.new
        # encodedHTML = coder.encode(page)
        puts page

     end

    def watched_link_params

        params.require(:default).permit(:url)

    end

end

Solution

  • Try this:

    def scrape
      @url = watched_link_params[:url]
    
      page = Nokogiri::HTML(open(@url))
      puts page
    end
    

    You will need to pass in the entire url, including the protocol designator; that is to say, you need to use http://www.google.com instead of www.google.com:

    >> params = ActionController::Parameters.new(default: {url: 'http://www.google.com'})
    >> watched_link_params = params.require(:default).permit(:url)
    >> @url = watched_link_params[:url]
    "http://www.google.com"
    >> page = Nokogiri::HTML(open(@url))