Net/HTTP get

Discussion in 'Ruby' started by Tim Mcd, Feb 4, 2009.

  1. Tim Mcd

    Tim Mcd Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Mcd, Feb 4, 2009
    #1
    1. Advertising

  2. Tim Mcd

    Mark Thomas Guest

    On Feb 3, 8:47 pm, Tim Mcd <> wrote:
    > 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.
     
    Mark Thomas, Feb 4, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hendy
    Replies:
    0
    Views:
    763
    hendy
    Dec 20, 2005
  2. serge calderara

    HTTP GET vs HTTP POST protocol

    serge calderara, Nov 4, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    611
  3. hendy
    Replies:
    1
    Views:
    267
    Bob Barrows [MVP]
    Dec 20, 2005
  4. n3d!m

    Http post and http get

    n3d!m, Jan 25, 2012, in forum: Python
    Replies:
    2
    Views:
    361
    n3d!m
    Feb 6, 2012
  5. Fred
    Replies:
    17
    Views:
    258
Loading...

Share This Page