下面就是这个 html解析类及用法,下面的功能是采集www.opendir.cn这个网站的百度收录数据,需要的朋友可以测试下
复制代码 代码如下:
_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+(+);
$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());
}
/**
* 获取内容
*/
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);
}
}
$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);
,