2013-10-16 2 views

답변

2
require 'open-uri' 
require 'prettyprint' 

open('http://www.iana.org/_img/2013.1/iana-logo-header.svg') do |f| 
    pp f.meta 
end 

실행 ... 블록에서 할 수 있으며 같은 것을 얻을 것이다 :

{"server"=>"Apache", 
"last-modified"=>"Fri, 04 Jan 2013 01:17:14 GMT", 
"content-type"=>"image/svg+xml", 
"content-length"=>"32870", 
"accept-ranges"=>"bytes", 
"date"=>"Wed, 16 Oct 2013 03:59:41 GMT", 
"x-varnish"=>"2012021384 2012020567", 
"age"=>"70", 
"via"=>"1.1 varnish", 
"connection"=>"keep-alive"} 

다음과 같이 실행하십시오.

require 'open-uri' 

last_modified = open('http://www.iana.org/_img/2013.1/iana-logo-header.svg') do |f| 
    f.last_modified 
end 
last_modified # => 2013-01-03 18:17:14 -0700 

완료되었습니다.

OpenURI's open은 블록을 허용합니다. 이 블록 안에는 현재 연결에 대한 정보를 반환하는 여러 메서드에 액세스 할 수 있습니다. 문서에서

:
open("http://www.ruby-lang.org/en") {|f| 
    f.each_line {|line| p line} 
    p f.base_uri   # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/> 
    p f.content_type  # "text/html" 
    p f.charset   # "iso-8859-1" 
    p f.content_encoding # [] 
    p f.last_modified # Thu Dec 05 02:45:02 UTC 2002 
}  

또한 last_modified로, 같은 추가적인 정보는 OpenURI::Meta 설명서를 참조하십시오.

2

당신은 별말씀을 ...하지만 open-uri을 사용할 수 없습니다 :

여기 내 코드입니다. 당신은뿐만 아니라 파일을 읽고 싶다면

require 'net/http' 

http = Net::HTTP.new('www.example.com', 80) 
resp = http.request_get('/image1.jpg') 
date = resp['last-modified'] 

, 당신은 그

http.request_get('/index.html') do |resp| 
    resp.read_body do |str| 
    # ... 
    end 
end 
+0

예, 이것은 OpenURI에서 사용할 수 있습니다. 설명서에서 ['last_modified'] (http://www.ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI/Meta.html#method-i-last_modified)를 참조하십시오. –

+0

니스, 결코 알아 차리지 못했습니다. –

1
require 'mechanize' 

agent = Mechanize.new 

modified_date = agent.get("http://example.com/image1.jpg").response["last-modified"] 
관련 문제