Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'nokogiri'
- require 'net/http'
- require 'open-uri'
- def fetch(uri_str, limit = 10)
- raise ArgumentError, 'too many HTTP redirects' if limit == 0
- response = Net::HTTP.get_response(URI(uri_str))
- case response
- when Net::HTTPSuccess then
- response.body
- when Net::HTTPRedirection then
- location = response['location']
- warn "redirected to #{location}"
- fetch(location, limit - 1)
- else
- response.value
- end
- end
- resp = fetch('сайт')
- Nokogiri::HTML.parse(resp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement