如何修复“Errno::ENOENT:没有这样的文件或目录@rb_sysopen”?

问题描述

编码的新手!我使用 rails new 创建了一个新项目。

我正在尝试抓取网站,但出现错误 Errno::ENOENT: No such file or directory @ rb_sysopen

require 'open-uri'
require 'nokogiri'
require 'pry'

def get_page
    link = "https://www.pokemon.com/us/pokedex/"
    doc = Nokogiri::HTML(open(link))
    #rest of code
end

get_page

感谢任何帮助。谢谢!

解决方法

link = "https://www.pokemon.com/us/pokedex/"
doc = Nokogiri::HTML(URI.open(link))

只需添加 URI

,

Kernel#open 打开文件、IO 流或子进程。它不会打开 URI:

open(path [,mode [,perm]] [,opt]) → io or nil

open(path [,opt]) {|io|块 } → 对象

创建一个连接到给定流、文件或子进程的 IO 对象。

OpenURI 通过调用 usedURI::open method

方法:URI.open

.open(name,*rest,&block)Object

允许打开各种资源,包括 URI。

因此,您的代码应如下所示:

def get_page
  link = "https://www.pokemon.com/us/pokedex/"
  doc = Nokogiri::HTML(URI.open(link))
  #rest of code
end