1

上面的代码有效,但只返回数组的第一个元素,可能是因为我在 $item 中设置了 0。如果我删除 0,我会收到“Array”消息。如何获得完整的循环并将其放入我的表格输出中?谢谢。

<?php
include_once('simple_html_dom.php');
$target_url = "http://www.theurlscraped.com";
$html = new simple_html_dom();
$html->load_file($target_url);

foreach($html->find('div[class=class0]') as $post) {

$item['url'] = $post->find('a.class1', 0)->href;
$item['image'] = $post->find('img.class2', 0)->src;
$item['descrizione'] = $post->find('span.class3', 0)->plaintext;
$item['price'] = $post->find('span.class4', 0);
}?>

编辑:要抓取的页面结构:

<div class="class0">
    <a class="class1" href="/another/page">
        <span class="class3">
            <span class="class6">
                Hello world!
            </span>
        Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore....
        </span>
        <span>
            <span>
                <span class="class4">tag</span>
                <span class="class5">tagtag</span>
            </span>
            <img class="class2" src="http://www.urlsourceimage.com/img.jpg">
        </span>
    </a>
    <a class="class1" href="/another/page">
        <span class="class3">
            <span class="class6">
                Hello world, this is me! 
            </span>
        Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
        </span>
        <span>
            <span>
                <span class="class4">tag1</span>
                <span class="class5">tagtag1</span>
            </span>
            <img class="class2" src="http://www.urlsourceimage.com/img1.jpg">
        </span>
    </a>

    ...

    <a class="class1" href="/another/page">
        <span class="class3">
            <span class="class6">
                Life should be fun for everyone!
            </span>
        Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </span>
        <span>
            <span>
                <span class="class4">tag2</span>
                <span class="class5">tagtag2</span>
            </span>
            <img class="class2" src="http://www.urlsourceimage.com/img2.jpg">
        </span>
    </a>
</div>

期望输出:

<table>
  <tr>
    <td rowspan="2"><a href="<?php echo $item['url'];?>"><img src="<?php echo $item['image'];?>" /></a></td>
    <td><a href="<?php echo $item['url'];?>">Price: <?php echo $item['price'];?></a></td>
  </tr>
  <tr>
    <td><?php echo $item['descrizione'];?></td>
  </tr>
  <tr>
    <td><a href="<?php echo $item['url'];?>">dettagli offerta &raquo;</a></td>
    <td><?php echo $item['price'];?></td>
  </tr>
</table>

编辑:我也尝试了这个解决方案,但返回和无限循环:

<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.theurlscraped.com');


foreach($html->find('div[class=class0]') as $table) {
$urls = $table->find('a.class1');
$images = $table->find('img.class2');
$descrizioni = $table->find('span.class3');

    foreach($urls as $url)
    foreach($images as $image)
    foreach($descrizioni as $descrizione)
    {
        echo "URL = " . $url->href ."<br />";
        echo "Img = " . $image->src ."<br />";
        echo "Descrizione = " . $descrizione ."<br />";
    }
}
?>
4

1 回答 1

0

您想抓取一个站点并在其中显示一些内容,这些内容显示在表格的 class0 中,您需要的是:

您需要在主 foreach 循环中请求查找的任何位置删除 0,并以这种方式围绕这些查找语句包装另一个循环:

foreach($post->find('a.class1') as $element)

您需要在这些内部循环下回显 $element ,将它们包装在方便的列和行中。

如果你不明白,请告诉我,我会在晚上不忙的时候把整件事写给你。谢谢

于 2012-08-30T08:35:10.797 回答