php正则匹配获取指定url网页页面超级链接地址

时间:2016.04.25 发布人:QQ312724262

php正则匹配获取指定url网页页面超级链接地址

已解决问题

谷歌QQ312724262用户在2016.04.25提交了关于“重阳节php正则匹配获取指定url网页页面超级链接地址”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-04-04T10:56:59。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,真心佩服你,谢谢 !

希望以下的回答,能够帮助你。

第1个回答

用户名:puigd2m5o**0  

在**采集与页来自面分析中,常需要抓取给定url页面的内容听编改深半,或者第二、第三层次深度页面内容。

这里是一个测试例问答子的实现,仅供参考。

代码如下:


/*
匹配给定页面链接
return:arraymatch[link,content丰,all]
*/
functionmatch_links($host,$document){
$仍海己核其叫pattern='/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/i';
preg_match_all($pattern,$document,$m);
return$m;

preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1)(.*?)1|([^s>]+))[^>]*>?(.*?)</a>'isx",$documen社好科t,$links);
while(list($key,$val)=内历端因each($links[步促德配至2])){
if(!e重危各温奏兰各机想mpty($val))
if(preg_match("/http/",$val)){
$m七精素门atch['link'][]=$val;
}
else{
$match['link'][]=$host.$val;
}
}
while(list($key,$val)=each($links[3]劳司散察候走粉八)){
if(!empty($val))
if(preg掉武须占冷办地肥顺解_match("/http/",$val)){
$match['link'][]=$val;
}
e伟包李临节lse{
$match['link'][]=$host.$val;
}
}
while(list($key,$val)=each($links[4])){
if(!empty($val))
$match['content'][]=$val;
}
while(list($key,$val)=each($links[0])){
if(!empty($val))
$match['all'][]=$val;
}
return$match['l毛房交画宪预ink'];
}

/*
从给定url中获取页面文本内容
*/
functionge速t_content_from_url($url){
$str=@file_get_contents($url);
if(mb_check_enco金ding($str拉老夫转南界束,"GBK"))
$str=iconv("GBK","UTF-8",$str);
$str=strip_tags($str);//过滤html标签
/*
$s粒祖月tr=preg_replace("@<script(.*?)</script>@is","",$str);
$str=preg_replace("@<iframe(.*?)</iframe>@is","",$str);
$str=preg_replace("@<style(.*?)</style>@is","",$str);
$str=preg_replace("@<(.*?)>@is","",$str);
*/
//过滤非汉字字符
preg_match_all('/[x{4e00}-x{9fff}]+/u',$str,$matches);
$str=join(',',$matches[0]);
if(!$str)
return**LL;

return$str;
}

functionget_content($url,$depth){
if(!$url||$depth<1)
returnfalse;

while($depth>1){
$str=@file_get_contents($url);
if(!$str)
returnfalse;

$parseurl=parse_url($url);
if($parseurl['host'])
$host=$parseurl[scheme]."://".$parseurl['host'];

$arrlink=match_links($host,$str);
$arr_url=array_unique($arrlink);

$depth--;
foreach($arr_urlas$url){
$content.=get_content($url,$depth);//递归调用
}
}

$content.=get_content_from_url($url);

return$content;
}