我有这个HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div><strong>this is note 2</strong>. it has a url.</div></en-note>
我想从中提取:
<div><strong>this is note 2</strong>. it has a url.</div>
目前,我这样做:
html_doc.xpath('//en-note').each do |a_tag|
p a_tag.content
end
这让我知道了:
"this is note 2. it has a url."
如何更改代码以便返回实际的HTML,而不仅仅是文本?
答案 0 :(得分:2)
只需使用to_s
ruby-1.9.2-p180 :010 > Nokogiri::HTML('<tag>content</tag>').xpath('//tag').first.content
=> "content"
ruby-1.9.2-p180 :004 > Nokogiri::HTML('<tag>content</tag>').xpath('//tag').to_s
=> "<tag>content</tag>"
答案 1 :(得分:0)
请使用此
html_doc.at_xpath('//en-note').to_xml