我想展示DEF,不包括ABC:
<ul class="view5">
<li>
<a href="/prd/detail_cate.jsp?item_cd=35386060" class="prdLink">
<span class="prd_wrap">
<img src="//itemimage.cjmall.com/goods_images/35/060/35386060J.jpg" alt="" onerror='this.src="http://itemimage.cjmall.com/goods_images/imageJ.jpg"'/>
</span>
<span class="prdTit">
<B><FONT COLOR=#FF0000>ABC</FONT></B>DEF
</span>
</a>
</li>
</ul>
我的代码是:
@html.xpath('//ul[contains(@class,"view5")]//li//a[contains(@class,"prdLink")]//span[contains(@class,"prdTit")]').text
如何修改我的代码以显示'DEF',不包括'ABC'?
答案 0 :(得分:0)
你使它变得比它更难。如果要显示“DEF”,则删除“ABC”。我会这样做:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<ul class="view5">
<li>
<a href="/prd/detail_cate.jsp?item_cd=35386060" class="prdLink">
<span class="prdTit">
<B><FONT COLOR=#FF0000>ABC</FONT></B>DEF
</span>
</a>
</li>
</ul>
EOT
doc.at('.prdTit b').remove
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> <ul class="view5">
# >> <li>
# >> <a href="/prd/detail_cate.jsp?item_cd=35386060" class="prdLink">
# >> <span class="prdTit">
# >> DEF
# >> </span>
# >> </a>
# >> </li>
# >> </ul>
# >> </body></html>