rails nokogiri no such file or directory
Asked Answered
C

3

16

Gemfile

...
gem 'nokogiri'
...

In controller

doc = Nokogiri::HTML(open('http://google.com'))

And I got a error

Errno::ENOENT in SiteController#scrap
No such file or directory - http://google.com
app/controllers/site_controller.rb:6:in `initialize'
app/controllers/site_controller.rb:6:in `open'
app/controllers/site_controller.rb:6:in `scrap'

I tried delete Gemfile.lock and do "bundle install" again, but it's not resolved my problems.

rails 2.3.8

ruby 1.9.3p194

What am I doing wrong? Thanks in advance for your help

Consol answered 15/11, 2012 at 8:1 Comment(0)
R
38

You need to require 'open-uri' if you want to pass URLs to open(). Additionally, you'll need to read the file after opening it:

require 'open-uri'
doc = Nokogiri::HTML(open('http://google.com').read)
Rubella answered 15/11, 2012 at 8:3 Comment(1)
I don't think you need to read the file though, it works without it for me.Quintile
H
1

In my case (ruby installed with brew), I do these steps to make open() works

require 'open-uri'

# to get a TempFile object
URI.open("http://www.google.com")

# to get string of html
URI.open("http://www.google.com").read

I got this from documentation at https://ruby-doc.org/stdlib-2.6.3/libdoc/open-uri/rdoc/OpenURI.html

Hauberk answered 26/3, 2021 at 11:58 Comment(1)
This was the problem for me. It now needs URI.open rather than just open. You may not need to require 'open-uri' if it's in your Gemfile and being auto-requiredArnelle
L
0

I was trying to access the http as a file in model.

For instance :
web_doc = Nokogiri::HTML(open("https://www.abokifx.com/", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE))
ERROR which was thrown 

Errno::ENOENT: No such file or directory @ rb_sysopen - https://www.abokifx.com/
    from (irb):10:in `initialize'
    from (irb):10:in `open'
    from (irb):10

As open is not able to read https URL as a file. OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP. It is possible to open an http, https or ftp URL as though it were a file, so you need to require 'open-uri'.

On adding require 'open-uri' Nokogiri was able to scrape the website.

Lecompte answered 13/8, 2017 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.