1
上面的代碼工作,但只返回數組的第一個元素,可能是因爲我在$ item中設置了0。如果我刪除0,我會收到「Array」消息。如何獲得完整的循環,並把它放在我的表格輸出?謝謝。html dom解析器php數組
<?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 />";
}
}
?>
感謝BeQI的快速回復。我會嘗試,但我很確定我會再次需要你的幫助:) – advalue
另一個快速的嘗試是,回聲刮的代碼,並用css處理它的表示,它比得到 - > href, - > src, - >明文一個接一個,並重寫整個事情 – serdarsenay
爲您的最後一個建議我認爲它不可能,因爲在刮代碼teere是相對鏈接(例如/ category/product/item1)不工作我的網站。 – advalue