php用xpath解析html的代码实例讲解

网络编程 2021-07-04 23:24www.168986.cn编程入门
在本篇文章里长沙网络推广给大家分享了关于php用xpath解析html的代码实例讲解,对此有需要的朋友们可以学习下。

实例1

$xml = simplexml_load_file('https://forums.eveonline.'); 
 
$names = $xml->xpath("html/body/p/p/form/p/p/p/p/p[]/p/p/table//tr/td[@class='icViews']"); 
foreach($names as $name) 
{ 
 echo $name . "<br/>"; 
}

实例2

$url = 'http://.baidu.';
$ch = curl_init();
curl_set($ch, CURLOPT_FILE, fopen('php://stdout', 'w'));
curl_set($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_set($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);
}

以上就是相关的2个实例内容,以及相关的代码, 感谢大家对狼蚁SEO的支持。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by