上面的代码有效,但只返回数组的第一个元素,可能是因为我在 $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 »</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 />";
}
}
?>