您好,欢迎访问一九零五行业门户网

网页爬虫 - 请问PHP怎么使用xpath解析html内容呢?

在网上查看了很多相关资料,但都是php用xpath解析xml的,请问php有没有相关的函数或是类库能解析html吗?谢谢
回复内容: 在网上查看了很多相关资料,但都是php用xpath解析xml的,请问php有没有相关的函数或是类库能解析html吗?谢谢
直接用zend-dom吧,方便多了!
http://framework.zend.com/manual/2.3/en/modules/zend.dom.query.html
引入不用教了吧?
$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);}
差不多这样的
其它类似信息

推荐信息