欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

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

发布时间:2024/9/27 php 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 PHP的html实现xpath解析,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 . "
";

}

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

}

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

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

总结

以上是生活随笔为你收集整理的PHP的html实现xpath解析,php用xpath解析html的代码实例讲解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。