問題描述
如何使用 DOMDocument 獲取此 html 中的 url (How to use DOMDocument to get url in this html)
我的html
<td class="end">
<span class="shopName">ダイワンテレコム</span><a href="http://kakaku.com/shop/3211/?used_pdid=K0000575280&uctrl=85427"><img src="http://img1.kakaku.k‑img.com/images/itemlist/itemv_btn_sinfo_l.gif" width="102" height="28" alt="詳細を見る"></a>
</td>
我想用DOMDocument來獲取http://kakaku.com/shop/3211/?used_pdid=K0000575280&uctrl=85427
我的php是
$atag = $td‑>getElementsByTagName("a");
$shop_url=$atag‑>getAttribute("href"); //PHP Fatal error: Call to undefined method DOMNodeList::getAttribute() in C:\xampp\htdocs\wp‑content\themes\theme‑child\cellphone.php on line 172
參考解法
方法 1:
You have put foreach for getElementsByTagName
its, a domlist object. Below code will be return all links.
<?php
$atag = $td‑>getElementsByTagName("a");
foreach( $atag as $searchNode )
{
echo $searchNode‑>getAttribute("href");
}
?>
方法 2:
Since getElementsByTagName
return a Traversable object (DOMNodeList
), you can access to the first element with
$atag[0]‑>getAttribute("href");
or loop them all with
foreach ($atag as $node) {
//$node‑>getAttribute("href");
}