栈解析html文件,利用栈将html源码解析为节点树
/// 如何利用棧將html解析成節點樹
/// 首先html是由一個個節點組成,最大的節點為節點?? 她有兩個子節點
和/// 首先我們將壓入棧中 再將
壓入棧中 遇到出棧? 壓入棧中 遇到 出棧 最終/// 出棧 可以看出 出棧的一定是棧頂元素的子節點
/// 本程序假定html完全規范
/// todo? 解決標簽未閉合問題
/// todo? 根據標簽名訪問
/// ?? ?? 根據id訪問
/// ?? ?? 根據class訪問
///?? ?? innerHtml 和 innerText
$tree_file = fopen("tree_file","w");
fclose($tree_file);
$siteurl="http://www.cnblogs.com/poissonnotes/archive/2010/05/28/1745996.html";
$str=get_content_by_url($siteurl);
$pattern="/<.>/";
$deep=0;
$global_false=true ;
$result=array();
$queue_id=0;
if(preg_match_all($pattern,$str,$matches))
{
$count=count($matches[0]);
$matches=$matches[0];
$queue=array();
$error = fopen("string_tagss","a");
for($i=0;$i
{
$temptag = $matches[$i];
$node["Id"]=$i;
$node["ChildId"]=array();
$node["html_tag"]=$temptag;
/// 如果是單標簽
if(is_single_tag($temptag))
{
//echo "this is a single tag:?? ?";
//echo $temptag."\n";
$str_deep=make_string($deep);
fwrite($error,$str_deep."\t".$temptag."\n");
if(!empty($queue))
{
$queue[count($queue)-1]["ChildId"][]=$queue_id;
}
$queue_id++;
$result[]=$node;
continue ;
}
/// 如果是結束標簽
if(is_end_tag($temptag))
{
$temp_node = array_pop($queue);
$queue[count($queue)-1]["ChildId"][]=$queue_id ;? // 出隊的節點一定是棧頂元素的子孩子
$queue_id++;
$result[]=$temp_node;
}
else
{
/// 如果是開始標簽
$str_deep=make_string($deep);
fwrite($error,$str_deep."\t".$temptag."\n");
$queue[] = $node;
$deep++;
}
}
if($global_false)
{
echo "ok\n" ;
}
else
{
echo "error\n" ;
}
//制作關系圖譜? 不包含結束標簽
//
//?? ?
//?? ??? ??? ?............
//?? ??? ?............
//?? ?
//????????????? ............
//????????????? ............
$deep = 0;
make_tree($result,$result[count($result)-1],$deep);
}
function is_single_tag($test_tag)
{
$single_tags=array("meta","img","link","input","!DOCTYPE","area","base","basefont","embed","hr","br");
$single_tag_string="(";
foreach($single_tags as $single_tag)
{
$single_tag_string=$single_tag_string.$single_tag."|"? ;
}
$single_tag_string=$single_tag_string.")";
$single_tag_string=str_replace("|)",")",$single_tag_string);
$pattern="/\/ i" ;
if(preg_match($pattern,$test_tag))
{
return true ;
}
return false ;
}
function is_end_tag($test_tag)
{
$pattern="/\/" ;
if(preg_match($pattern,$test_tag))
{
return true ;
}
return false ;
}
function is_exist_start_tag($queue,$temptag)
{
$end_string=str_replace("/","",$temptag);
$end_string=preg_replace("/\s*/","",$end_string);
$end_string=strtolower($end_string);
$count=count($queue);
echo $count."\n";
var_dump($queue);
for($i=$count-1;$i>=0;$i--)
{
echo $i."\n";
$string = $queue[$i]["html_tag"];
$string = preg_replace("/()/",'$1$2$3',$string);
if(strtolower($string) == strtolower($end_string))
{
return true;
}
}
return false ;
}
function is_pear_tag($start_tag,$temptag)
{
$start_string=preg_replace("/()/",'$1$2$3',$start_tag);
$end_string=str_replace("/","",$temptag);
$end_string=preg_replace("/\s*/","",$end_string);
if(strtolower($start_string) == strtolower($end_string))
{
return true ;
}
else
{
return false ;
}
}
function make_string($deep)
{
$temstr="";
for($i=0;$i
$temstr=$temstr."-";
return $temstr;
}
function judge_tag($last_tag,$temptag)
{
return false;
// 這里做一個判斷 判斷上一個標簽是否能嵌套這個標簽 比如
//
不嵌套//
不嵌套//
不嵌套//
不嵌套//
//
不嵌套//
- 不嵌套
//
//
不能嵌套}
function get_content_by_url($site_url)
{
$ch=curl_init($site_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
$content=curl_exec($ch);
$content = str_replace("\r\n","",$content);
$content = str_replace("\n\r","",$content);
$content = str_replace("\n","",$content);
$content = str_replace("\r","",$content);
$content = preg_replace("//is","",$content);
$content = preg_replace("//is","",$content);
$content = preg_replace("/
$content = preg_replace("/
$errores = fopen("baidu","w");
fwrite($errores,$content) ;
fclose($errores);
return $content ;
}
function make_tree($result,$node,$deep)
{
// 制作前綴 使得顯示的層次分明
$str="" ;
for($i=0;$i<$deep;$i++)
{
$str=$str."--";
}
echo $str;
echo $node["html_tag"]."\n";
$write_str = $str.$node["html_tag"]."\n";
$tree_file = fopen("tree_file","a");
fwrite($tree_file,$write_str);
fclose($tree_file);
$deep++;
if(!empty($node["ChildId"]))
{
foreach($node["ChildId"] as $Child)
{
make_tree($result,$result[$Child],$deep) ;?? ///遞歸
}
}
else
{
return ;
}
}
?>
總結
以上是生活随笔為你收集整理的栈解析html文件,利用栈将html源码解析为节点树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用计算机或图形计算器在,图形计算器在函
- 下一篇: 计算机应用基础在线作业南开,2017南开