0

I am trying to get an image from the response body. Right now this gives me the entire HTML page. I see the tag but cannot specifically retrieve it. Any help would be great!

#Get Request
encoded_response = response.body.force_encoding("UTF-8")
url = URI.parse(encoded_response)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.img

For those about to ask, I had to encode the response because I was getting a Bad URI errorIP

mikeymurph77
  • 752
  • 1
  • 11
  • 28

1 Answers1

2

Have you looked at a parsing library like Nokogiri?

html = Nokogiri::HTML.parse(response.body.force_encoding("UTF-8"))
image_urls = html.css('img').map { |image_tag| image_tag["src"] }

For "downloading" the image, see here: Download an image from a URL?

Community
  • 1
  • 1
AndyV
  • 3,696
  • 1
  • 19
  • 17