2

我正在使用 curl 功能从另一端获取信息,但它显示了网站的完整页面。我想根据班级名称显示内容。请让我知道我该怎么做?我正在使用以下代码:-

 <?php
 $curl = curl_init('http://www.insolvency.govt.nz/cms/search?SearchableText=smith');
 curl_setopt($curl, CURLOPT_FAILONERROR, true);
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
 $result = curl_exec($curl);
 $dom = new DOMDocument();
 $res=$dom->loadHTML($result);
 $divs = $dom->getElementsByTagName('div');

 foreach($divs as $div) {
 if ($div->getAttribute('id') === 'searchResult') {
     echo $div->nodeValue;
 }else{
echo "error";   
}
}
?>
4

1 回答 1

2

XPath 可用于按类名获取元素。下面的示例将获取具有myClass该类的元素。

$dom = new DOMDocument();
$res=$dom->loadHTML($result);

$xpath = new DomXPath($dom);
$class = 'myClass';
$divs = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");

foreach($divs as $div) {
    echo $div->nodeValue;

    echo $dom->saveXML($div);
}
于 2013-08-02T09:11:08.043 回答