Php 使用类名解析curl结果
我正在使用curl函数从另一边获取信息,但它显示的是站点的完整页面。我想显示基于类名的内容。请告诉我怎么做? 我正在使用以下代码:-Php 使用类名解析curl结果,php,curl,html-parsing,domdocument,Php,Curl,Html Parsing,Domdocument,我正在使用curl函数从另一边获取信息,但它显示的是站点的完整页面。我想显示基于类名的内容。请告诉我怎么做? 我正在使用以下代码:- <?php $curl = curl_init('http://www.insolvency.govt.nz/cms/search?SearchableText=smith'); curl_setopt($curl, CURLOPT_FAILONERROR, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATI
<?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";
}
}
?>
可以使用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);
}
谢谢,它只显示文本,缺少超链接。我可以用html获取结果吗?请参见我的编辑,您可以使用
$dom->saveXML($div)
获取html。