Простой HTML Dom - PullRequest
       7

Простой HTML Dom

1 голос
/ 14 марта 2011

Спасибо, что нашли время, чтобы прочитать мой пост ... Я пытаюсь извлечь некоторую информацию с моего сайта, используя Simple HTML Dom ...

У меня есть чтение из источника HTML, хорошо, теперьЯ просто пытаюсь извлечь информацию, которая мне нужна.У меня такое чувство, что я поступаю об этом неправильно ... Вот мой сценарий ...

<?php

include_once('simple_html_dom.php');

// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");

// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);

$html = file_get_html('http://myshop.com/small_houses.html');
$html .= file_get_html('http://myshop.com/medium_houses.html');
$html .= file_get_html('http://myshop.com/large_houses.html');

    //Define my variable for later
    $product['image'] = '';
    $product['title'] = '';
    $product['description'] = '';

foreach($html->find('img') as $src){

    if (strpos($src->src,"http://myshop.com") === false) {
        $src->src = "http://myshop.com/$src->src";
    }
       $product['image'] = $src->src;
}

foreach($html->find('p[class*=imAlign_left]') as $description){
       $product['description'] =  $description->innertext;
}

foreach($html->find('span[class*=fc3]') as $title){
       $product['title'] =  $title->innertext;
}

echo $product['img'];
echo $product['description'];
echo $product['title'];

?>

Я положил конец эхо ради тестирования ... но я неполучить что-нибудь ... Любые указатели были бы отличной ПОМОЩЬ!

Спасибо

Чарльз

1 Ответ

2 голосов
/ 14 марта 2011

file_get_html() возвращает объект HTMLDom, и вы не можете объединить объекты, хотя в HTMLDom есть методы __toString, когда их объединение более чем несколько раз повреждено, попробуйте следующее:

<?php

include_once('simple_html_dom.php');

// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
// for readability purposes
//header("Content-Type: text/plain");

// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);

$pages = array(
    'http://myshop.com/small_houses.html',
    'http://myshop.com/medium_houses.html',
    'http://myshop.com/large_houses.html'
)


foreach($pages as $page)
{
    $product = array();
    $source = file_get_html($page);

    foreach($source->find('img') as $src)
    {
        if (strpos($src->src,"http://myshop.com") === false)
        {
            $product['image'] = "http://myshop.com/$src->src";
        }
    }

    foreach($source->find('p[class*=imAlign_left]') as $description)
    {
        $product['description'] =  $description->innertext;
    }

    foreach($source->find('span[class*=fc3]') as $title)
    {
        $product['title'] =  $title->innertext;
    }

    //debug perposes!

    echo "Current Page: " . $page . "\n";
    print_r($product);
    echo "\n\n\n"; //Clear seperator
}
?>
...