我使用簡單的HTML DOM從HTML文檔中提取數據,並且我有幾個需要幫助的問題。使用簡單的HTML遍歷表格DOM
在隨
if ($td->find('a'))
我想單獨提取href和錨節點的內容,並將其放置在單獨的變量開頭的行。但是代碼不起作用(請參閱下面代碼中的回聲輸出)。這樣做的最好方法是什麼?請注意,我的目的是在後面的信息中創建一個XML文檔,所以我需要按照正確的順序排列信息。
鏈接導致包含有關不同汽車的詳細信息(例如「最高速度」,「價格」等)的頁面,我也想將其提取並放入單獨的變量中。我怎樣才能獲得這些網頁上的數據?
<?php include 'simple_html_dom.php'; $html = new simple_html_dom(); $html = file_get_html('http://www.example.com/foo.html'); $items = array(); foreach ($html->find('table') as $table) { foreach ($table->find('tr') as $tr) { foreach ($tr->find('td') as $td) { if ($td->find('a')) { $link = $td->find('a.href'); echo $link; // empty $text = $td->find('a.text'); echo $text; // Array } else { echo 'Name: ' . $td; } } } }
HTML文檔看起來是這樣的:
<div>
<table>
<tr>
<td>
<a href="car1.html" target="_blank">Car 1</a>
</td>
<td>
Porsche
</td>
</tr>
<tr>
<td>
<a href="car2.html" target="_blank">Car 2</a>
</td>
<td>
Chrysler
</td>
</tr>
... and so on...
你能否納入我們對我們答案的建議並更新你的問題? –