Rails 不会用 nokogiri 和 open-uri 读取 link
Rails won't read a link with nokogiri and open-uri
我有一个控制器,它获取一个 url 作为参数传递,我正试图在 url 处抓取整个页面。但是当我尝试阅读 url 时,出现以下错误:No such file or directory @ rb_sysopen - www.google.com
控制器:
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(9..@url.length-3)
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
试试这个:
def scrape
@url = watched_link_params[:url]
page = Nokogiri::HTML(open(@url))
puts page
end
您需要传递整个 url,包括协议指示符;也就是说,你需要使用 http://www.google.com
而不是 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))
我有一个控制器,它获取一个 url 作为参数传递,我正试图在 url 处抓取整个页面。但是当我尝试阅读 url 时,出现以下错误:No such file or directory @ rb_sysopen - www.google.com
控制器:
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(9..@url.length-3)
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
试试这个:
def scrape
@url = watched_link_params[:url]
page = Nokogiri::HTML(open(@url))
puts page
end
您需要传递整个 url,包括协议指示符;也就是说,你需要使用 http://www.google.com
而不是 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))