使用PHP5和XPath轻松地进行抓取和HTML解析
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP5和XPath轻松地进行抓取和HTML解析相关的知识,希望对你有一定的参考价值。
This example uses file_get_contents to retrieve remote html. From there, we can parse through it using php5's DOMDocument and DOMXpath. XPath Queries are easy to create using the Firefox extension "XPather"
<?php //a URL you want to retrieve $my_url = 'http://www.digg.com'; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); //Put your XPath Query here $my_xpath_query = "/html/body/div[@id='container']/div[@id='contents']/div[@class='list' and @id='wrapper']/div[@class='main' and position()=1]/div[contains(@class, 'news-summary')]/div[@class='news-body']/h3"; $result_rows = $xpath->query($my_xpath_query); //here we loop through our results (a DOMDocument Object) foreach ($result_rows as $result_object){ echo $result_object->childNodes->item(0)->nodeValue; } ?>
以上是关于使用PHP5和XPath轻松地进行抓取和HTML解析的主要内容,如果未能解决你的问题,请参考以下文章