复制代码 代码如下:
<?php
$oldtting = libxml_u_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_t(‘ur_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’)===fal)
{腊肉的腌制方法
$content = file_get_contents($url);
}
el
{
$ch = curl_init();山东省简称
$ur_agent = “baiduspider+(+http://www.baidu.com/arch/spider.htm)”;
$ur_agent1=’mozilla/5.0 (windows nt 5.1; rv:6.0) gecko/20100101 firefox/6.0′;
curl_topt($ch, curlopt_url, $url);
curl_topt($ch, curlopt_header, fal);
curl_topt($ch, curlopt_returntransfer, 1);
curl_topt($ch, curlopt_referer, $url);
curl_topt($ch, curlopt_uragent, $ur_agent1);
curl_topt($ch, curlopt_followlocation,1);
$content =curl_exec($ch);
curl_clo($ch);
}
$html = new domdocument();
$html->loadhtml($content);
$this->_xpath = new domxpath( $妈妈发现儿子女友不是人html );
//return $this;
}
p献血日ublic function find($query, $index = null)
{
if($this->_nodepath == ”)
$this->_nodepath = ‘//’;
el
$this->_nodepath .= ‘/’;
$nodes = $this-&g沈阳药科大学t;_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;
el
return fal;
}
/**
* 获取属性值
*/
public function getattribute($name)
{
if ($this->_nodepath != ” && $this->_xpath != null )
return $this->_xpath->query($this->_nodepath)->item(0)->getattribute($name);
el
return fal;
}
public function __get($name)
{
if($name == ‘innertext’)
return $this->text();
el
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);
本文发布于:2023-04-06 09:42:58,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/52bdefac24a4046bbf4e40c90f78e85d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php实现的一个很好用HTML解析器类可用于采集数据.doc
本文 PDF 下载地址:php实现的一个很好用HTML解析器类可用于采集数据.pdf
留言与评论(共有 0 条评论) |