简单的 HTML Dom

发布于 2024-10-21 14:28:30 字数 1415 浏览 2 评论 0原文

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

?>

为了测试,我将 echo 放在末尾...但我没有得到任何东西...任何指针都会是一个很大的帮助!

谢谢

查尔斯

Thanks for taking the time to read my post... I'm trying to extract some information from my website using Simple HTML Dom...

I have it reading from the HTML source ok, now I'm just trying to extract the information that I need. I have a feeling I'm going about this in the wrong way... Here's my script...

<?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'];

?>

I put echo's on the end for sake of testing...but I'm not getting anything... Any pointers would be a great HELP!

Thanks

Charles

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-10-28 14:28:30

file_get_html() 返回一个 HTMLDom 对象,并且您无法连接对象,尽管 HTMLDom 有 __toString 方法,当连接的对象以某种方式超过 lilly 损坏时,请尝试以下操作:

<?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
}
?>

file_get_html() returns a HTMLDom Object, and you cannot concatenate Objects, although HTMLDom have __toString methods when there concatenated there more then lilly corrupt in some way, try the following:

<?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
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文