2

I'm using Simple HTML DOM to extract data from a HTML document, and I have a couple of issues that I need some help with.

  1. On the line that begins with if ($td->find('a')) I want to extract the href and the content of the anchor node separately, and place them in separate variables. The code however doesn't work (see output from echoes in the code below).

    What is the best way to do this? Note that my purpose is to create a XML document out of the information later on, so I need the information in the correct order.

  2. The links leads to pages containing detailed information about the different cars (e.g. "Max speed", "Price" etc) that I also want to extract and put into separate variables. How can I get hold of data on these pages?

    <?php
    include 'simple_html_dom.php';
    
    $html = new simple_html_dom();
    $html = file_get_html('http://www.example.com/foo.html');
    
    $items = array();
    
    foreach ($html->find('table') as $table) {
        foreach ($table->find('tr') as $tr) {
    
            foreach ($tr->find('td') as $td) {
    
                if ($td->find('a')) {
                    $link = $td->find('a.href');
                    echo $link;  // empty
    
                    $text = $td->find('a.text');
                    echo $text; // Array
                }
                else {
                    echo 'Name: ' . $td;
                }
            }
        }
    }
    

The HTML document looks like this:

<div>
    <table>
        <tr>
            <td>
                <a href="car1.html" target="_blank">Car 1</a>
            </td>
            <td>
                Porsche
            </td>
        </tr>
        <tr>
            <td>
                <a href="car2.html" target="_blank">Car 2</a>
            </td>
            <td>
                Chrysler
            </td>
        </tr>
        ... and so on...
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
holyredbeard
  • 19,619
  • 32
  • 105
  • 171

2 Answers2

4

Use $td->find('a', 0)->href and $td->find('a', 0)->innertext to access element attributes in the first case, and contents in the second. Also, if there might be multiple anchor to be found, use 0 as a safe guard to always get the first one.

Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
2

'a.href' is the selector to look for an anchor tag with the CSS class href. Not to get the href attribute of the anchor tag. You can do that like this:

$link = $td->find('a', 0);
$href = $link->href;
Paul
  • 139,544
  • 27
  • 275
  • 264
  • 1
    I always wondered if there were any active users of this 3d party class, it's nice to see, that there are a few :) – Morgan Wilde Nov 18 '12 at 22:45