Nokogiri(RubyGem):查找并替换HTML标记
我有以下HTML:
Foo
The quick brown fox.
Bar
Jumps over the lazy dog.
…并且通过使用RubyGem Nokogiri ( hpricot替换),我想将其更改为以下HTML:
Foo
The quick brown fox.
Bar
Jumps over the lazy dog.
换句话说:如何使用Nokogiri查找和替换某些HTML标记? 我知道如何找到它们(使用css关键字),但我不知道在解析文档时如何替换它们。
谢谢你的帮助!
试试这个:
require 'nokogiri' html_text = "Foo
The quick brown fox.
Bar
Jumps over the lazy dog.
" frag = Nokogiri::HTML(html_text) frag.xpath("//h1").each { |div| div.name= "p"; div.set_attribute("class" , "title") }
看起来这样做是对的:
require 'rubygems' require 'nokogiri' markup = Nokogiri::HTML.parse(<<-somehtml) Foo
The quick brown fox.
Bar
Jumps over the lazy dog.
somehtml markup.css('h1').each do |el| el.name = 'p' el.set_attribute('class','title') end puts markup.to_html # >> # >> # >> Foo
# >> The quick brown fox.
# >> Bar
# >> Jumps over the lazy dog.
# >>
#!/usr/bin/env ruby require 'rubygems' gem 'nokogiri', '~> 1.2.1' require 'nokogiri' doc = Nokogiri::HTML.parse <<-HERE Foo
The quick brown fox.
Bar
Jumps over the lazy dog.
HERE doc.search('h1').each do |heading| heading.name = 'p' heading['class'] = 'title' end puts doc.to_html