1
我試圖解析與結構如下表:解析HTML表使用PHP - 檢查類名
我使用PHP爲:
$html=file_get_contents("https://www.somesite.com/index");
$dom = new DOMDocument;
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('tbody') as $table) {
$rows = $table->getElementsByTagName('tr');
foreach ($rows as $row) {
unset($col_array);
$cols = $row->getElementsByTagName('td');
foreach ($cols as $col) {
$col_array[]=$col->textContent;
}
$row_array[]=$col_array;
}
print_r ($row_array);
}
的問題是,我想只抓住「crc-row closed」類的'tr'元素。
我試圖添加一個if語句:
if ($rows->getAttribute('class')=="crc-row closed")
但它返回我一個錯誤:
致命錯誤:調用未定義的方法的DOMNodeList ::的getAttribute()在
任何人都可以幫我嗎?
謝謝!
難道你不想要的第一個項目出來的$行,而不是整個$行列表?你可以試試if($ rows [0] - > getAttribute('class')==「space8」)或if(current($ rows) - > getAttribute('class')==「space8」)'或在你的循環中if'($ row-> getAttribute('class')==「crc-row closed」)'單行$ – Scuzzy
不工作...我得到另一個錯誤:不能使用DOMNodeList類型的對象作爲array – Almazini
你究竟在哪裏添加了if子句? –