我在 ruby on rails 中使用 hpricot gem 来解析网页并提取元标记内容。但是如果网站在<noscrpit>
标签之后有一个标签,<head>
它会抛出异常
Exception: undefined method `[]' for nil:NilClass
我什至尝试将 gem 更新到最新版本。但还是一样。
这是我使用的示例代码。
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 之前,我正在考虑删除 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>