ruby on rails で hpricot gem を使用して、Web ページを解析し、メタタグの内容を抽出します。ただし、Web サイトの<noscrpit>
タグの直後に<head>
タグがある場合、例外がスローされます。
Exception: undefined method `[]' for nil:NilClass
宝石を最新バージョンに更新しようとさえしました。それでも同じです。
これは私が使用するサンプルコードです。
require 'rubygems'
require 'hpricot'
require 'open-uri'
begin
index_page = Hpricot(open("http://sample.com"))
puts index_page.at("/html/head/meta[@name='verification']")['content'].gsub(/\s/, "")
rescue Exception => e
puts "Exception: #{e}"
end
hpricot に Web ページを提供する前に、noscript タグを削除することを考えていました。またはそれを行う他の方法はありますか??
私のHTMLスニペット
<html>
<head>
<noscript>
<meta http-equiv="refresh" content="0; url=http://www.yoursite.com/noscripts.html"/>
</noscript>
<meta name="verification" content="7ff5e90iormq5niy6x98j75-o1yqwcds-c1b1pjpdxt3ngypzdg7p80d6l6xnz5v3buldmmjcd4hsoyagyh4w95-ushorff60-f2e9bzgwuzg4qarx4z8xkmefbe-0-f" />
</head>
<body>
<h1>Testing</h1>
</body>
</html>