$xp = new xf_htmldom(); $xp->loadhtml('http://dealer.bitauto.com/100040078/cars.html'); $rows = $xp->find('dl/dd/a', 0)->innertext; print_r($rows); _xpath = $xpath; $this->_nodepath = $nodepath; } public function loadhtml($url) { ini_set('user_agent', 'mozilla/5.0 (linux; u; android 2.1; en-us; nexus one build/erd62) applewebkit/530.17 (khtml, like gecko) version/4.0 mobile safari/530.17 –nexus'); $content = ''; if(strpos(strtolower($url), 'http')===false) { $content = file_get_contents($url); } else { $ch = curl_init(); $user_agent = baiduspider+(+http://www.baidu.com/search/spider.htm); $user_agent1='mozilla/5.0 (windows nt 5.1; rv:6.0) gecko/20100101 firefox/6.0'; curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_header, false); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_referer, $url); curl_setopt($ch, curlopt_useragent, $user_agent1); curl_setopt($ch, curlopt_followlocation,1); $content =curl_exec($ch); curl_close($ch); } $html = new domdocument(); $html->loadhtml($content); $this->_xpath = new domxpath( $html ); return $this; } public function find($query, $index = null) { if($this->_nodepath == '') $this->_nodepath = '//'; else $this->_nodepath .= '/'; $nodes = $this->_xpath->query($this->_nodepath.$query); if ($index == null && !is_numeric($index)) { $tmp = array(); foreach ($nodes as $node) { $tmp[] = new xf_htmldom($this->_xpath, $node->getnodepath()); } return $tmp; } return new xf_htmldom($this->_xpath,$this->_xpath->query($this->_nodepath.$query)->item($index)->getnodepath()); } /** * 获取内容 */ public function text() { if ($this->_nodepath != '' && $this->_xpath != null ) return $this->_xpath->query($this->_nodepath)->item(0)->textcontent; else return false; } /** * 获取属性值 */ public function getattribute($name) { if ($this->_nodepath != '' && $this->_xpath != null ) return $this->_xpath->query($this->_nodepath)->item(0)->getattribute($name); else return false; } public function __get($name) { if($name == 'innertext') return $this->text(); else return $this->getattribute($name); }}
复制代码