phploopsweb-scrapingdomsimple-html-dom

Using Simple HTML DOM to Scrape?


Simple HTML DOM is basically a php you add to your pages which lets you have simple web scraping. It's good for the most part but I can't figure out the manual as I'm not much of a coder. Are there any sites/guides out there that have any easier help for this? (the one at php.net is a bit too complicated for me at the moment) Is there a better place to ask this kind of question?

The site for it is at: http://simplehtmldom.sourceforge.net/manual.htm

I can scrape stuff that has specific classes like <tr class="group">, but not for stuff that's in between. For example.. This is what I currently use...

$url = 'http://www.test.com';
$html = file_get_html($url);
foreach($html->find('tr[class=group]') as $result)
  {
    $first = $result->find('td[class=category1]',0);
    $second = $result->find('td[class=category2]',0);
    echo $first.$second;
  }
}

But here is the kind of code I'm trying to scrape.

<table>
  <tr class="Group">
    <td>
      <dl class="Summary">
        <dt>Heading 1</dt>
          <dd><a href="#123" class="ViewProfile">Cat</a></dd>
          <dd><a href="#032" class="ViewProfile">Bacon</a></dd>
        <dt>Heading 2</dt>
          <dd><a href="#143" class="ViewProfile">Narwhal</a></dd>
          <dd><a href="#642" class="ViewProfile">Ice Soap</a></dd>
      </dl>
    </td>
  </tr>
</table>

I'm trying to extract the content of each <dt> and put it to a variable. Then I'm trying to extract the content of each <dd> and put it to a variable, but nothing I tried works. Here's the best I could find, but it gives me back only the first heading repeatedly rather than going to the second.

foreach($html->find('tr[class=Summary]') as $result2)
  {
    echo $result2->find('dt',0)->innertext;
  }

Thanks to anyone who can help. Sorry if this is not clear or that it's so long. Ideally I'd like to be able to understand these DOM commands more as I'd like to figure this out myself rather than someone here just do it (but I'd appreciate either).

TL;DR: I am trying to understand how to use the commands listed in the manual (url above). The 'manual' isn't easy enough. How do you go about learning this stuff?


Solution

  • I think $result2->find('dt',0) gives you back element 0, which is the first. If you omit that, you should be able to get an array (or nodelist) instead. Something like this:

    foreach($html->find('tr[class=Summary]') as $result2)
    {
        foreach ($result2->find('dt') as $node)
        {
           echo $node->innertext;
        }
    }
    

    You don't strictly need the outer for loop, since there's only 1 tr in your document. You could even leave it altogether to find each dt in the document, but for tools like this, I think it's a good thing to be both flexible and strict, so you are prepared for multiple rows, but don't accidentally parse dts from anywhere in the document.