【问题标题】:Ruby/Rails eqv PHP get_meta_tagsRuby/Rails eqv PHP get_meta_tags
【发布时间】:2009-08-03 17:10:34
【问题描述】:

PHP get_meta_tags. 是否有等效的 ruby​​/RoR 方法

我希望读取给定 url 的元标记信息

【问题讨论】:

    标签: php ruby-on-rails ruby equivalent


    【解决方案1】:

    您可以尝试使用Hpricot 并执行以下操作:

    doc = Hpricot(URI.parse("http://example.com/").read)
    (doc/'/html/head/meta')
      #=> Elements[...]
    

    【讨论】:

    • 是的,我曾考虑过使用屏幕抓取实用程序,但我希望有一个内置的 Ruby 方法,可能带有 HTTP::Net 或其他东西。我将只使用 Nokogiri,因为它已经安装在我的开发机器上。谢谢
    【解决方案2】:

    非常感谢。

    它对我有用。我正在尝试获取描述形式的元标记。 我的代码是这样的

    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
    

    【讨论】:

    • 也可以写:meta_desc = (doc/'/html/head/meta').find {|meta| meta.get_attribute('name') == "description"}; description = meta_desc.nil? : "" ? meta_desc.get_attribute('content')
    猜你喜欢
    • 2016-04-05
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2011-07-29
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多