title: XPath extract attribute from <div> in PHP
body: <p>i want to extract an attribute from an  and display its value.</p>
  <pre><code>&lt;div class=&quot;b-text-4xl b-text-btc-first b-font-bold btcecc-animated liveup livedown&quot; data-price=&quot;38696.15125182&quot; data-live-price=&quot;bitcoin&quot; data-rate=&quot;1&quot; data-currency=&quot;USD&quot; data-timeout=&quot;1610051644181&quot;&gt;&lt;span&gt;38,696.15&lt;/span&gt; &lt;b class=&quot;fiat-symbol&quot;&gt;$&lt;/b&gt;&lt;/div&gt;
  </code></pre>
  <p>I need the value of &quot;data-price&quot;.
  The location of the full html is at <a href="https://www.btc-echo.de/kurs/bitcoin/" rel="nofollow noreferrer">https://www.btc-echo.de/kurs/bitcoin/</a></p>
  <p>I tried this:</p>
  <pre><code>    $url = &quot;https://www.btc-echo.de/kurs/bitcoin/&quot;;
      libxml_use_internal_errors(true);
  
      $doc = new DOMDocument;
      $doc-&gt;loadHTML(utf8_encode(file_get_contents($url)));
      $xpath = new DOMXpath($doc);
  
      foreach ($xpath-&gt;query('*[@id=&quot;main&quot;]/div[1]/div[3]/div[2]/div[1]/div/div/div[1]/div/@data-price') as $textNode) {
          echo $textNode-&gt;nodeValue;
      }
  
  </code></pre>