1

投稿をお読みいただきありがとうございます...SimpleHTMLDomを使用してWebサイトから情報を抽出しようとしています...

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 に答える