Net/HTTP get

T

Tim Mcd

doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
images = (doc/"img")
count = 0

Net::HTTP.start("redhanded.hobix.com") do |http|
images.each do |img|
filename = img['src'].match(/([^\/]+\..+)/)
resp = http.get("#{img["src"]}")
open("#{filename[1]}", "w") do |file|
file.write(resp.body)
end
count += 1
end
end

So thats fairly simple, because images on _why's blog are stored as
such: redhanded.hobix.com/images/whatever.w/e

But how would I grab an image if it was, say, on slashdot.com?
(images.slashdot.com/image.jpg)

With my understanding, doing just an http.get("images.slashdot.etc.")
wouldn't work, because it bases it off of the .start? Any help is
appreciated!
 
M

Mark Thomas

doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
images = (doc/"img")
count = 0

Net::HTTP.start("redhanded.hobix.com") do |http|
  images.each do |img|
    filename = img['src'].match(/([^\/]+\..+)/)
    resp = http.get("#{img["src"]}")

If it's a full URL then you just have to create a uri object:
http.get(URI.parse(img['src']))

- Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top