Search code examples
phpsimple-html-dom

Scraping with simple_html_dom


I am trying to scrape this:

<a id="pa1">Site1</a>
<font size="-1">Text1</font><br />
<font size="-1" color="green">Text2</font><br />

I get get to pa1 easily..but I want to get to the two fonts that come after.. So I used this:

$html = new simple_html_dom();
$html->load($document);

foreach ($html->find('#pa1>font') as $e) {
    $this->check_line_two = $this->process_array_elements($e->innertext);
}

foreach ($html->find('#pa1>font>font') as $e) {
    $this->check_line_three = $this->process_array_elements($e->innertext);
}

Both didn't work. How can I get the next element with simple html dom?


Solution

  • Like feeela said, those font elements are not descendants of the anchor. Try something like this:

    foreach ($html->find('#pa1') as $e) {
        $firstFontElement = $e->next_sibling();
    }