我想显示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'?
你把事情复杂化了。如果你想显示"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>