开发者

how can you manipulate an html page parsed via Nokogiri?

so i parsed an html page using nokogiri.

i wa开发者_JAVA百科nt to wrap tags around each occurence of links

.wrap() doesn't appear to work properly.

puts doc.xpath("//a").wrap("<b></b>");

returns just plain regular unchanged html.


This is a flaw in the way wrap works. Here is the source:

# File lib/nokogiri/xml/node_set.rb, line 212
  def wrap(html, &blk)
    each do |j|
      new_parent = Nokogiri.make(html, &blk)
      j.parent.add_child(new_parent)
      new_parent.add_child(j)
    end
    self
  end

As you can see, instead of replacing j with new_parent, it adds new_parent to the end of j's siblings. You can do what you want like this:

doc.search('//a').each do |j|
  new_parent = Nokogiri::XML::Node.new('b',doc)
  j.replace  new_parent
  new_parent << j
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜