Ruby/Rails eqv PHP get_meta_标记
PHPRuby/Rails eqv PHP get_meta_标记,php,ruby-on-rails,ruby,equivalent,Php,Ruby On Rails,Ruby,Equivalent,PHPget\u meta\u标记是否有一个等效的ruby/RoR方法。 我希望阅读给定url的元标记信息,您可以尝试使用并执行以下操作: doc = Hpricot(URI.parse("http://example.com/").read) (doc/'/html/head/meta') #=> Elements[...] 您可以尝试使用并执行以下操作: doc = Hpricot(URI.parse("http://example.com/").read) (doc/'/htm
get\u meta\u标记是否有一个等效的ruby/RoR方法。
我希望阅读给定url的元标记信息,您可以尝试使用并执行以下操作:
doc = Hpricot(URI.parse("http://example.com/").read)
(doc/'/html/head/meta')
#=> Elements[...]
您可以尝试使用并执行以下操作:
doc = Hpricot(URI.parse("http://example.com/").read)
(doc/'/html/head/meta')
#=> Elements[...]
非常感谢
这对我有用。我正在尝试从元标记中获取描述。
我的代码是
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
非常感谢
这对我有用。我正在尝试从元标记中获取描述。
我的代码是
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
是的,我曾考虑过使用screen scrape实用程序,但我希望有一个内置的Ruby方法,可能是HTTP::Net之类的。我将只使用Nokogiri,因为它已经安装在我的开发机器上。谢天谢地,我曾考虑过使用screen scrape实用程序,但我希望有一个内置的Ruby方法,可能是HTTP::Net之类的。我将只使用Nokogiri,因为它已经安装在我的开发机器上。感谢您还可以这样写:
meta_desc=(doc/'/html/head/meta')。查找{meta | meta.get_属性('name')=“description”};description=meta_desc.nil?:"" ? meta|desc.get|u attribute('content')
也可以写:meta|desc=(doc/'/html/head/meta')。查找{meta | meta.get|u attribute('name')=“description”};description=meta_desc.nil?:"" ? 元描述获取属性(“内容”)