我正在尝试解析维基词典条目以检索所有英语定义。我能够检索所有定义,问题是某些定义是其他语言的。我想做的是以某种方式仅检索具有英文定义的 HTML block 。我发现,如果有其他语言条目,可以通过以下方式检索英语定义之后的 header :
header = (doc/"h2")[3]
所以我只想搜索此标题元素之前的所有元素。我认为使用 header.preceding_siblings()
可能可以,但这似乎不起作用。有什么建议吗?
最佳答案
您可以通过 Nokogiri 使用访客模式。此代码将删除从其他语言定义的 h2 开始的所有内容:
require 'nokogiri'
require 'open-uri'
class Visitor
def initialize(node)
@node = node
end
def visit(node)
if @remove || @node == node
node.remove
@remove = true
return
end
node.children.each do |child|
child.accept(self)
end
end
end
doc = Nokogiri::XML.parse(open('http://en.wiktionary.org/wiki/pony'))
node = doc.search("h2")[2] #In this case, the Italian h2 is at index 2. Your page may differ
doc.root.accept(Visitor.new(node)) #Removes all page contents starting from node
关于ruby - 在 hpricot/nokogiri 中搜索 h2 元素之前的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452443/