DomCrawler复杂的div嵌套

时间:2015-05-15 14:26:48

标签: symfony dom

我在使用dom抓取工具尝试获取某些数据时遇到了麻烦。 我想得到'鳄梨'这个名字和'1.50英镑'我虽然能做类似的事情

 $message = $crawler->filterXPath('h3')->text(); 



  <div class="product"> 
  <div class="productInner"> 
  <div class="productInfoWrapper"> 
  <div class="productInfo"> 
  <h3> 
  <a href="http://website.com" > 
  Avocado  
  <img src="pic.jpg" alt="" /> 
  </a> 
  </h3> 
  </div> 
  </div> 
 <div class="pricingAndTrolleyOptions"> 
 <div class="pricing"> 
<p class="pricePerUnit"> 
 £1.50<abbr title="per">/</abbr><abbr title="unit"><span                 class="pricePerUnitUnit">unit</span></abbr> 
   </p> 
 <p class="pricePerMeasure">£1.50<abbr 
title="per">/</abbr><abbr 
title="each"><span class="pricePerMeasureMeasure">ea</span></abbr> 
 </p> 
</div> 
</div>
</div>

1 个答案:

答案 0 :(得分:1)

获取h3文本:

$message = $crawler->filterXPath('//div[@class="productInfo"]/h3')->text();

获得价格(即类别价格测量):

$price= $crawler->filterXPath('//p[@class="pricePerMeasure"]')->text();