php用xpath解析html的代码实例讲解
实例1
$xml=simplexml_load_file('https://forums.eveonline.com');
$names=$xml->xpath("html/body/p/p/form/p/p/p/p/p[*]/p/p/table//tr/td[@class='topicViews']");
foreach($namesas$name)
{
echo$name."
";
}
实例2
$url='http://www.baidu.com';
$ch=curl_init();
curl_setopt($ch,CURLOPT_FILE,fopen('php://stdout','w'));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_URL,$url);
$html=curl_exec($ch);
curl_close($ch);
//createdocumentobjectmodel
$dom=newDOMDocument();
//loadhtmlintodocumentobjectmodel
@$dom->loadHTML($html);
//createdomxpathinstance
$xPath=newDOMXPath($dom);
//getallelementswithaparticularidandthenloopthroughandprintthehrefattribute
$elements=$xPath->query('//*[@id="lg"]/img/@src');
foreach($elementsas$e){
echo($e->nodeValue);
}
以上就是相关的2个实例内容,以及相关的代码,感谢大家对毛票票的支持。