旗下导航:搜·么
当前位置:网站首页 > PHP教程 > 正文

php应用xpath剖析html的代码示例【php教程】

作者:搜搜PHP网发布时间:2019-11-26分类:PHP教程浏览:112


导读:本篇文章给人人带来的内容是关于php应用xpath剖析html的代码示例,有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。实例1$xml=simp...

本篇文章给人人带来的内容是关于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($names as $name) 
{ 
    echo $name . "<br/>"; 
}

实例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);

// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="lg"]/img/@src');
foreach ($elements as $e) {
  echo ($e->nodeValue);
}

以上就是php应用xpath剖析html的代码示例的细致内容,更多请关注ki4网别的相干文章!

标签:php