Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.52 KB | None | 0 0
  1. require 'nokogiri'
  2. require 'net/http'
  3. require 'open-uri'
  4.  
  5. def fetch(uri_str, limit = 10)
  6.   raise ArgumentError, 'too many HTTP redirects' if limit == 0
  7.   response = Net::HTTP.get_response(URI(uri_str))
  8.  
  9.   case response
  10.     when Net::HTTPSuccess then
  11.       response.body
  12.     when Net::HTTPRedirection then
  13.       location = response['location']
  14.       warn "redirected to #{location}"
  15.       fetch(location, limit - 1)
  16.     else
  17.       response.value
  18.   end
  19. end
  20.  
  21.  
  22. resp = fetch('сайт')
  23. Nokogiri::HTML.parse(resp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement