php用xpath解析html的代码实例讲解
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-08 11:51:49 浏览: 评论:0
在本篇文章里小编给大家分享了关于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.phpfensi.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);
- }
Tags: xpath php解析html
相关文章
- ·php+xml编程之xpath的应用实例(2021-05-08)
- ·PHP操作XML中XPath的应用示例(2021-12-01)
- ·PHP抓取网页、解析HTML常用的方法总结(2021-06-06)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)