Nokogiriでurlをparseするときは普通こんなコードになります。
doc = Nokogiri::HTML(open('http://example.com/'))
しかし接続先のプロトコールがhttpsの場合はNo such file or directoryのエラーとなります。
一旦net/httpsで取ってからそれをnokogiriでparseするような工夫が必要です。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/https' | |
require 'nokogiri' | |
url = "https://example.com" | |
url = URI.parse( url ) | |
http = Net::HTTP.new( url.host, url.port ) | |
http.use_ssl = true if url.port == 443 | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if url.port == 443 | |
path = url.path | |
path += "?" + url.query unless url.query.nil? | |
res, data = http.get( path ) | |
case res | |
when Net::HTTPSuccess, Net::HTTPRedirection | |
# parse link | |
doc = Nokogiri::HTML(data) | |
# do what you want ... | |
else | |
return "failed" + res.to_s | |
end |