2010-08-26 30 views
2

我正在使用機械化來抓取很好地工作的網站,但是由於您無法通過鏈接判斷它鏈接到的文件類型,例如, http://somesite.com/images.php?get=123 是否可以只下載標題?僅下載文件的標頭

我在問這個,因爲我想根據文件類型來決定是否下載它。 此外,它會幫助下載時決定一個文件名。

它不必使用機械化,但有沒有Rails這樣做的方式?

回答

1

您可以使用路邊

ruby-1.8.7-p174 > require 'rubygems' 
=> true 
ruby-1.8.7-p174 > require 'curb' 
=> true 
ruby-1.8.7-p174 > c = Curl::Easy.http_head('https://encrypted.google.com/images/logos/ssl_logo_lg.gif'){|easy| easy.follow_location = true} 
ruby-1.8.7-p174 > c.perform 
=> true 
=> #<Curl::Easy https://encrypted.google.com/images/logos/ssl_logo> 
ruby-1.8.7-p174 > c.content_type 
=> "image/gif"