Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing HTML the DOM way
    text
    copied!<p>we've got an ancient (internal) website with static info. We're going to replace it with something better, therefore I need to fetch all info. I <em>used</em> to do this via regex, but lately I stumbled about a few articles stating that using regex to parse info from HTML is <a href="http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html" rel="nofollow">inviting cthulhu to this realm</a>.</p> <p>So I decided to learn a few new tricks, start over and do it the DOM-way. the HTML part I need looks like this:</p> <pre><code>&lt;table id="articles"&gt; &lt;tr&gt; &lt;th&gt; &lt;a href='articles/aa123.html'&gt;&lt;img src="/iamges/aa123.jpg" alt="some article"&gt;&lt;/a&gt; &lt;br /&gt;short description &lt;/th&gt; &lt;td&gt; &lt;table class='details'&gt; &lt;tr&gt;&lt;th&gt;&lt;a href='articles/aa123.html'&gt;Some Article&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Type:&lt;/th&gt;&lt;td&gt;article type&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Price:&lt;/th&gt;&lt;td&gt;€ 99&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Manufacturer:&lt;/th&gt;&lt;td&gt;Some Company&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;th&gt;Warehouse:&lt;/th&gt;&lt;td&gt;x&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>And so far I got this:</p> <pre><code>$dom = new DOMDocument(); @$dom-&gt;loadHTMLFile ($file); $xpath = new DOMXPath($dom); $query = "/html/body/table[@id='articles']//th"; //catch all TH's $data = $xpath-&gt;evaluate($query); </code></pre> <p>And that's about where I get stuck. I know all content of the returned TH's is in the ChildNodes, but I'm having a hard time getting the values. I need the URL to the details page and the value for the Price column.</p> <p>How do I get those extracted?</p> <p>Currently I came up with the following:</p> <pre><code>$query = '//table[@class="details"]//td'; $data= $xpath-&gt;evaluate($query); $c = $ths-&gt;length; for ($i = 0; $i &lt; $c; $i++) { echo htmlentities($data-&gt;item($i)-&gt;nodeValue); } </code></pre> <p>But this only displays the text values from the TD's. When the content is a link, it only show the link-title. Not the URL.</p> <p><strong>UPDATE</strong> Thanks to Fab's suggestion I managed to book some progress. Currently I got the following:</p> <pre><code>$tables = $xpath-&gt;query('//table[@class="details"]'); foreach($tables as $table) { $url = $xpath-&gt;evaluate('//th/a/@href', $table); $articleName= $xpath-&gt;evaluate('//th/a', $table); $Manufacturer= $xpath-&gt;evaluate('//th[text()="Manufacturer:"]/../td', $table); echo 'articleName:' . $articleName . ' &lt;br /&gt;'; echo 'Manufacturer:' . $Manufacturer. ' &lt;br /&gt;'; echo 'url:' . $url. ' &lt;br /&gt;'; echo '&lt;br /&gt;'; } </code></pre> <p>But for some reason it always displays the data from the first acticle (repeated for as many articles as there are on the page). As if the 'foreach' statement always returns the 1st found table. Any tips?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload