php实现的一个很好用HTML解析器类可用于采集数据
程序员文章站
2022-05-15 11:05:42
复制代码 代码如下:
<?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解析器
* @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());
}
/**
* 获取内容
*/
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);
复制代码 代码如下:
<?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解析器
* @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());
}
/**
* 获取内容
*/
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);
上一篇: php 读取文件头判断文件类型的实现代码