Created
June 21, 2011 08:58
-
-
Save kinopyo/1037492 to your computer and use it in GitHub Desktop.
nokogiri parse https url
This file contains 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment