2010-11-19 21 views
1

我想下載與http.get(字符串),其中字符串是網址的圖片。字符串是不知道的,並且每天都在變化。我得到了過去的覆蓋。如果我打印字符串並將其粘貼到http.get中,它可以正常工作。 爲什麼http.get不關心變量字符串?http.get與字符串

require 'net/http' 
Net::HTTP.start("apod.nasa.gov") { |http| 
    resp = http.get("/apod/") 
    resp.body.each{|line| 
    if(line =~ /.jpg/) 
    line.sub!("<a href=","") 
    line.gsub!("\"","") 
    resp = http.get(line) 
    open("pic.jpg", "wb") { |file| 
     file.write(resp.body) 
    } 
    break; 
    end 
    } 
} 

子與

resp = http.get('/image/1011/cygnusNeb_geissinger1200.jpg') 

和它的作品...

回答

0

好,我知道了。該字符串行應該被刪除

require 'net/http' 
Net::HTTP.start("apod.nasa.gov") { |http| 
    resp = http.get("/apod/") 
    resp.body.each{|line| 
    if(line =~ /.jpg/) 
    line.sub!("<a href=","") 
    line.gsub!("\"","") 
    line = "/" + line.chomp 
    resp = http.get(line) 
    open("apod.jpg", "wb") { |file| 
     file.write(resp.body) 
    } 
    break; 
    end 
    } 
}