1

感谢您花时间阅读我的帖子...我正在尝试使用 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'];

?>

为了测试,我把回声放在最后......但我什么也没得到......任何指针都会是一个很好的帮助!

谢谢

查尔斯

4

1 回答 1

2

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
}
?>
于 2011-03-14T16:37:02.883 回答