使用php简单网页抓取和内容分析,php 模仿蜘蛛抓取内容并分析

该代码段展示了如何模拟百度和谷歌抓取网页内容。通过file_get_contents和curl方法获取页面,然后使用正则表达式匹配HTML标签,提取title、meta(包括keywords和description)以及body的内容。这段代码对于理解网页抓取和信息解析具有实践意义。
摘要由CSDN通过智能技术生成

这是一款模仿baidu,google抓取你网页时的样子哦,下面就是代码看看吧。

header("Content-Type:text/html;charset=gbk");

$message=$_POST['message'];

$contents = @file_get_contents("$message");

if($contents=="Forbidden"){

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, "$message");

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$contents = curl_exec($ch);

curl_close($ch);

}

preg_match_all("/

/is",$contents,$title);

preg_match_all("//is",$contents,$meta);

preg_match_all("/

/is",$contents,$body);

echo 'title:'.strip_tags($title[0][0]).'
';

for($i=0;$i

if(preg_match("/keywords/i",$meta[0][$i])){

preg_match_all("/content="(.*?)"/is",$meta[0][$i],$keywords);

}

if(preg_match("/description/i",$meta[0][$i])){

preg_match_all("/content="(.*?)"/is",$meta[0][$i],$description);

}

}

echo 'keywords:'.strip_tags($keywords[1][0]).'
';

echo 'description:'.strip_tags($description[1][0]).'
';

echo 'body:'.strip_tags($body[0][0]);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值