2012-01-21 53 views

回答

9

如果它僅僅是一個<img>用網址:

PAGE = "http://site.com/page.html" 
require 'nokogiri' 
require 'open-uri' 
html = Nokogiri.HTML(open(PAGE)) 
src = html.at('.profilePic img')['src'] 
File.open("foo.png", "wb") do |f| 
    f.write(open(src).read) 
end 

如果你需要把一個相對圖像路徑爲絕對,請參閱:
https://stackoverflow.com/a/4864170/405017

+0

謝謝。這有幫助! –

2

的懶惰的方式是使用機械化,因爲它會爲你找出網址和文件名:

require 'mechanize' 
agent = Mechanize.new 
doc = agent.get(url) 
agent.get(doc.parser.at('.profilePic img')['src']).save