簡體   English   中英

解析表,使用DOMXpath不能獲得超過3行

[英]Parsing A Table, Can't get more than 3 row Using DOMXpath

由於某些目前無法理解的奇怪原因,我無法從頁面中的表中獲取超過3行

這是頁面。

http://www.reedmfgco.com/en/products/cutters-and-cutter-wheels/cutter-wheels/cutter-wheels-for-tubing-cutters-plastic/

我想解析底部的表格。

由於頁面中只有一個表,因此我使Xpath非常簡單。 $xpath -> query('//tr')

如果我執行以下操作

echo $xpath -> query('//tr')->lenght;

我得到3

為什么我得到3那里有9行,我應該得到9


編輯這是我使用的代碼

$Dom = new DOMDocument();
@$Dom -> loadHTML($this->html);
$xpath = new DOMXPath($Dom);
echo $xpath -> query('//tr')->lenght;

並且請注意,$ this-> html是我文章中上一個鏈接的原始html。

此頁面上的HTML源代碼不適用於XML。 如果您打開頁面的源代碼並尋找標簽<tr> ,則它也包含3個元素。 表格行產品沒有開頭標簽<tr>

對於此問題,可以使用正則表達式來規范化表的內容。

$html = file_get_contents('http://www.reedmfgco.com/en/products/cutters-and-cutter-wheels/cutter-wheels/cutter-wheels-for-tubing-cutters-plastic/');

preg_match('`<tbody>(.*)<\/tbody>`', $html, $matches);
if (!empty($matches)) {
    $tableBody = str_replace('</tr><td', '</tr><tr><td', $matches[1]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM