Search code examples
phphtmldomdomdocument

Navigating in DOMDocument


I am trying to extract some data out of an e-commerce website for analysis. I use the DOMDocument() method but I can't manage to reach the price (9.90€) and the product name (Coffret bougies P'tits biscuits).

The following code is 24 times in the same page on which I previously did a:

$doc = new DOMDocument();
@$doc->loadHTML($page2);

What would be the DOM path to get the price and the product name?

        <div
  id="product-138156"
  class="blockProduct"
  onmouseover="javascript:layoverShow('138156');"
  onmouseout="javascript:layoverHide('138156');"
>
  <div
    class="block-product-hover"
    id="product-hover-138156"
    style="display: none;"
  >
    <div class="block-product-hover-shadow-top png_fix"></div>
    <div class="block-product-hover-shadow-bg png_fix">
      <div class="picture">
        <div id="wrap" style="top:0px;z-index:9999;position:relative;">
          <a
            class="cloud-zoom"
            href="/FR/fr/produits/fiche/coffret-bougies-ptits-biscuits-138156.htm"
            rel="'bigImage': '/images/cache//e/3/-e3c5743524d99741099da77a3dff9789_w310_h310.jpg'"
            style="position: relative; display: block;"
          >
            <img
              title="Coffret bougies P'tits biscuits"
              alt="Coffret bougies P'tits biscuits"
              src="http://cdn.maisonsdumonde.com//images/cache//e/3/-e3c5743524d99741099da77a3dff9789_w170_h170.jpg"
              height="170"
              width="170"
              aptsrc="/images/blank.gif"
              style="display: block;"
            />
          </a>
          <div
            class="mousetrap"
            style="background-image:url(/images/blank.gif);z-index:999;position:absolute;width:170px;height:170px;left:0px;top:0px;"
          ></div>
        </div>
      </div>
      <div class="details">
        <h3>
          <a
            href="/FR/fr/produits/fiche/coffret-bougies-ptits-biscuits-138156.htm"
            >Coffret bougies P'tits biscuits</a
          >
        </h3>

        <div
          class="starBlock_s"
          itemscope=""
          itemtype="http://data-vocabulary.org/Review-aggregate"
        ></div>
        <div class="action">
          <div>
            <span class="availability">En stock</span>
            <strong>9,90&nbsp;€</strong>
          </div>

          <a
            onclick="add_cart_liste_product('MTM4MTU2','MTM4MTU2','0.18722700 1388075230','MTM4MTU2','FR','fr','00000190',1); return wasaTrack('',this,200,function(data,link){ _gaq.push([ '_trackEvent', data.category, data.action ]);}, { 'category': 'AjoutPanier', 'action': 'Listes' })"
            class="btn-cart png_fix"
            >Ajouter au panier</a
          >
        </div>
      </div>
      <div class="desc">
        <p>Hauteur : 1.6 cm <br />Largeur : 4.7 cm <br />Profondeur : cm</p>
        <span class="hr">&nbsp;</span>
        <p>
          <a
            href="/FR/fr/produits/fiche/coffret-bougies-ptits-biscuits-138156.htm"
            >Transformez votre intérieur avec nos objets Déco Maisons du
            Monde.</a
          >
        </p>
        <p>
          <a
            class="more"
            href="/FR/fr/produits/fiche/coffret-bougies-ptits-biscuits-138156.htm"
            >En savoir +</a
          >
        </p>
      </div>
    </div>
    <div class="block-product-hover-shadow-bottom png_fix"></div>
  </div>
  <div class="picture">
    <a href="/FR/fr/produits/fiche/coffret-bougies-ptits-biscuits-138156.htm">
      <img
        title="Coffret bougies P'tits biscuits"
        alt="Coffret bougies P'tits biscuits"
        src="http://cdn.maisonsdumonde.com//images/cache//e/3/-e3c5743524d99741099da77a3dff9789_w170_h170.jpg"
        height="170"
        width="170"
        aptsrc="/images/blank.gif"
      />
    </a>
  </div>
  <div class="details">
    <h3>
      <a href="/FR/fr/produits/fiche/coffret-bougies-ptits-biscuits-138156.htm"
        >Coffret bougies P'tits biscuits</a
      >
    </h3>

    <div
      class="starBlock_s"
      itemscope=""
      itemtype="http://data-vocabulary.org/Review-aggregate"
    ></div>
    <div class="action">
      <p>
        <span class="availability">En stock</span>
        <strong>9,90&nbsp;€</strong>
      </p>

      <a
        onclick="add_cart_liste_product('MTM4MTU2','MTM4MTU2','0.22488000 1388075230','MTM4MTU2','FR','fr','00000190',1); return wasaTrack('',this,200,function(data,link){ _gaq.push([ '_trackEvent', data.category, data.action ]);}, { 'category': 'AjoutPanier', 'action': 'Listes' })"
        class="btn-cart png_fix"
        >Ajouter au panier</a
      >
    </div>
  </div>
</div>

Solution

  • For information I finally used :

    preg_replace("/[^0-9,.]/", "",str_replace(",",".",substr($linker->nodeValue, 0, -5)))