[英]How to parse multiple elements in portions for html via Simple Html Dom
我正在嘗試在li內部獲取各種元素,如下所示。 我對此很陌生,所以我可能沒有使用最有效的方法,但這就是我開始的地方...
示例代碼已簡化。
<li id='entry_0' title='09879879'>
<div ....>
<h2> The title text would go here </h2>
<span class='entrySize' ....> 20oz </span>
<span class='entryPrice' ....> $32.09 </span>
<span class='anotherEntry' ....> More Data I need To Grab </span>
.......
</div>
</li>
<li> .... With same structure as above .... 100's of entries like this </li>
我知道如何分別拉各個部分,但是很難在html的一部分中進行分組。
$filename = "directory/file.html";
$html = file_get_html($filename);
for($i=0; $i<=count(entryNumber);$i++)
{
$li_id = "entry_".$i;
foreach($html->find('li[id='.$li_id.']') as $li) {
echo $li->innertext;
}
}
因此,這使我獲得了訂單項代碼中具有ID號作為唯一屬性的內容。 我想在遍歷訂單項標記時抓取h2文本,entrySize,entryPrice等。 我不了解的是,一旦我獲得了訂單項廣告代碼的內容,該如何解析該訂單項內部廣告代碼和屬性。 完整HTML文檔中可能還有其他部分,這些標簽具有與整個文檔中相同的ID,類的標記,因此我將其細分為多個部分,而不是一次解析每個部分。
我也想從li標簽的title標簽中拉出title屬性。
希望我的解釋有意義。
您可能應該使用DOM解析器。 PHP附帶了一個捆綁包,您可以使用許多其他捆綁包。
<?php
$html = file_get_content($page);
$doc = new DOMDocument();
$doc->loadHTML($html);
// now find what you need
$items = $dom->getElementsByTagName('li');
foreach ($items as $item) {
$id = $item->getAttribute('id');
if (strpos($id, 'item_') !== false) {
// found matchin li, grab its children
}
}
以此為基准,我們無法為您編寫所有代碼。 查看PHP文檔以完成此操作:)從目前為止,您需要遵循這些文檔以使其獲取子值並進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.