如何使用 Nokogiri 替换外部标签
How to replace outer tags using Nokogiri
我正在尝试使用 Nokogiri 替换 HTML 节点的外部标签,其中检测它的最可靠方法是通过其子节点之一。
之前:
<div>
<div class="smallfont" >Quote:</div>
Words of wisdom
</div>
之后:
<blockquote>
Words of wisdom
</blockquote>
以下代码片段检测到我要查找的元素,但我不确定如何从那里继续:
doc = Nokogiri::HTML(html)
if doc.at('div.smallfont:contains("Quote:")') != nil
q = doc.parent
# replace tags of q
# remove first_sibling
end
它工作正常吗?
doc = Nokogiri::HTML(html)
if quote = doc.at('div.smallfont:contains("Quote:")')
text = quote.next # gets the ' Words of wisdom'
quote.remove # removes div.smallfont
puts text.parent.replace("<blockquote>#{text}</blockquote>") # replaces wrapping div with blockquote block
end
我会这样做:
require 'nokogiri'
doc = Nokogiri::HTML(DATA.read)
smallfont_div = doc.at('.smallfont')
smallfont_div.parent.name = 'blockquote'
smallfont_div.remove
puts doc.to_html
__END__
<div>
<div class="smallfont" >Quote:</div>
Words of wisdom
</div>
这导致:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<blockquote>
Words of wisdom
</blockquote>
</body></html>
<blockquote>
里面的白色space在显示的时候会被浏览器吞掉,所以一般不会有问题,但是有些浏览器还是会显示前导spaceand/or尾随space.
如果你想清理包含 "Words of wisdom" 的文本节点,那么我会这样做:
smallfont_div = doc.at('.smallfont')
smallfont_parent = smallfont_div.parent
smallfont_div.remove
smallfont_parent.name = 'blockquote'
smallfont_parent.content = smallfont_parent.text.strip
这导致:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<blockquote>Words of wisdom</blockquote>
</body></html>
或者,这将生成相同的结果:
smallfont_div = doc.at('.smallfont')
smallfont_parent = smallfont_div.parent
smallfont_parent_content = smallfont_div.next_sibling.text
smallfont_parent.name = 'blockquote'
smallfont_parent.content = smallfont_parent_content.strip
代码的作用应该很容易理解,因为 Nokogiri 的方法是不言自明的。
我正在尝试使用 Nokogiri 替换 HTML 节点的外部标签,其中检测它的最可靠方法是通过其子节点之一。
之前:
<div>
<div class="smallfont" >Quote:</div>
Words of wisdom
</div>
之后:
<blockquote>
Words of wisdom
</blockquote>
以下代码片段检测到我要查找的元素,但我不确定如何从那里继续:
doc = Nokogiri::HTML(html)
if doc.at('div.smallfont:contains("Quote:")') != nil
q = doc.parent
# replace tags of q
# remove first_sibling
end
它工作正常吗?
doc = Nokogiri::HTML(html)
if quote = doc.at('div.smallfont:contains("Quote:")')
text = quote.next # gets the ' Words of wisdom'
quote.remove # removes div.smallfont
puts text.parent.replace("<blockquote>#{text}</blockquote>") # replaces wrapping div with blockquote block
end
我会这样做:
require 'nokogiri'
doc = Nokogiri::HTML(DATA.read)
smallfont_div = doc.at('.smallfont')
smallfont_div.parent.name = 'blockquote'
smallfont_div.remove
puts doc.to_html
__END__
<div>
<div class="smallfont" >Quote:</div>
Words of wisdom
</div>
这导致:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<blockquote>
Words of wisdom
</blockquote>
</body></html>
<blockquote>
里面的白色space在显示的时候会被浏览器吞掉,所以一般不会有问题,但是有些浏览器还是会显示前导spaceand/or尾随space.
如果你想清理包含 "Words of wisdom" 的文本节点,那么我会这样做:
smallfont_div = doc.at('.smallfont')
smallfont_parent = smallfont_div.parent
smallfont_div.remove
smallfont_parent.name = 'blockquote'
smallfont_parent.content = smallfont_parent.text.strip
这导致:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<blockquote>Words of wisdom</blockquote>
</body></html>
或者,这将生成相同的结果:
smallfont_div = doc.at('.smallfont')
smallfont_parent = smallfont_div.parent
smallfont_parent_content = smallfont_div.next_sibling.text
smallfont_parent.name = 'blockquote'
smallfont_parent.content = smallfont_parent_content.strip
代码的作用应该很容易理解,因为 Nokogiri 的方法是不言自明的。