且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Nokogiri html解析问题

更新时间:2023-12-05 09:17:52

你说得对,问题是text.text 返回开始标记和结束标记之间的文本.由于元标签是空的,这会给你空字符串.您需要内容"属性的值.

You're correct, the problem is text. text returns the text between the opening tag and the closing tag. Since meta-tags are empty, this gives you the empty string. You want the value of the "content" attribute instead.

doc.xpath("//meta[@name='Keywords']/@content").each do |attr|
  puts attr.value
end

既然你知道只有一个名为keywords"的元标签,你实际上不需要遍历结果,而是可以像这样直接取第一项:

Since you know that there will be only one meta-tag with the name "keywords", you don't actually need to loop through the results, but can take the first item directly like this:

puts doc.xpath("//meta[@name='Keywords']/@content").first.value

但是请注意,如果没有名为content"的元标记,这将导致错误,因此第一个选项可能更可取.

Note however, that this will cause an error if there is no meta-tag with the name "content", so the first option might be preferable.