Home >Backend Development >PHP Tutorial >使用DOMDocument抓取数据 并分类

使用DOMDocument抓取数据 并分类

WBOY
WBOYOriginal
2016-06-23 14:27:151232browse

http://smart-phones.biyixia.com/node/1013

<?php/** * Created by PhpStorm. * User: Admin * Date: 13-11-14 * Time: 下午10:42 */$link[]=array();$page_url='http://smart-phones.biyixia.com/node/1013';//$content=get_data($page_url);$doc= new DOMDocument();@$doc->loadHTMLFile($page_url);$div_contents = $doc->getElementsByTagName('div');print_r($div_contents);foreach($div_contents as $div_content ){    $items = $div_content->getAttribute("class");    $itme_value = $items->nodeValue;}echo $items_value;


我想要的效果是 比如: 手机尺寸=>XXXX
                  产品价格=>XXXX

跪求指导!!!


回复讨论(解决方案)

$link[]=array();$page_url='http://smart-phones.biyixia.com/node/1013';//$content=get_data($page_url);$doc= new DOMDocument();@$doc->loadHTMLFile($page_url);$xPath = new  DOMXPath($doc);$div_tags = $xPath->evaluate("/html/body//div//@class");for($i=0;$i<$div_tags->length;$i++){    $div_tag=$div_tags->item($i);    $div_contents=$div_tag->nodeValue;    echo $div_contents;    echo "<br>";}


使用xpath也能到标签里的内容

<div class="col-xs-3 key">图像尺寸</div>


比如想要拿出图像尺寸 怎么搞??????人呢。。。。

你都会xpath,还要问么?
自己用firebug或类似插件查一查就知道xpath了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn