有没有办法从图片 URL 中检索最后修改日期?
我可以从浏览器中看到最后修改日期,我认为最后修改日期在 HTTP header 内。
提前致谢。
这是我的代码:
image_file = open('http://www.example.com/image1.jpg')
最佳答案
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
接受一个 block 。在该 block 内,您可以访问不同的方法,这些方法将返回有关当前连接的信息。
来自文档:
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
}
另见 OpenURI::Meta附加信息的文档,例如 last_modified
。
关于ruby - 有没有办法从 Ruby 中的图像 URL 检索上次修改日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393146/