Copy Code code as follows:
<?php
$oldSetting = Libxml_use_internal_errors (true);
Libxml_clear_errors ();
/**
*
* -+-----------------------------------
* | PHP5 Framework-2011
* | Web Site:www.iblue.cc
* | E-mail:mejinke@gmail.com
* | Date:2012-10-12
* -+-----------------------------------
*
* @desc HTML Parser
* @author Jingke
*/
Class Xf_htmldom
{
Private $_xpath = null;
Private $_nodepath = ';
Public function __construct ($xpath = null, $nodePath = ')
{
$this->_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);
echo $nodes->item (0)->getnodepath (); exit;
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 ());
}
/**
* Get content
*/
Public Function text ()
{
if ($this->_nodepath!= ' && $this->_xpath!= null)
return $this->_xpath->query ($this->_nodepath)->item (0)->textcontent;
Else
return false;
}
/**
* Get Property value
*/
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);
}
}
$XP = new Xf_htmldom ();
$xp->loadhtml (' http://www.aizhan.com/siteall/www.opendir.cn/');
$rows = $xp->find ("td[@id = ' Baidu ']/a", 0)->innertext;
Print_r ($rows);