views:

136

answers:

2

Is there an equivalent ruby/RoR method for PHP get_meta_tags.

I am looking to read the meta tag information of a given url

+4  A: 

You could try using Hpricot and do something like:

doc = Hpricot(URI.parse("http://example.com/").read)
(doc/'/html/head/meta')
  #=> Elements[...]
BaroqueBobcat
Yeah, I had thought about using a screen scrape utility, but I was hoping there was a built in Ruby method with maybe HTTP::Net or something. I will just use Nokogiri since it is already installed on my dev machines. Thanks
ErsatzRyan
+1  A: 

Thanks a lot.

It work for me. I am trying to get description form meta tag. my code is like

def self.extract_description_from_url(url)
  description = ""
  doc = Hpricot(URI.parse(url).read)
  (doc/'/html/head/meta').each do |meta|
    val=  meta.get_attribute('name')
    if val == "description" 
      description = meta.get_attribute('content')
    end
  end
  return description
end
Dinesh Atoliya
could also write: `meta_desc = (doc/'/html/head/meta').find {|meta| meta.get_attribute('name') == "description"}; description = meta_desc.nil? : "" ? meta_desc.get_attribute('content')`
glenn jackman