php 实现信息采集(网页内容抓取)程序代码

php 实现信息采集(网页内容抓取)程序代码


<?
//采集首页地址
$url="http://www.xz-src.com/";
//获取页面代码
$rs=file_get_contents($url);
//设置匹配正则
//$fp=fopen("text.txt","a");
//$fw=fwrite($fp,$rs);
//fclose($fp);
/*<I class=titles><A
href="http://www.xz-src.com/"
target=_blank>留住你身边的好男人</A></I>*/
$preg='/<i\s+class=\"titles\"><a\s+href=\"[^>]+\">(.*)<\/a><\/i>/i';
//进行正则搜索
preg_match_all($preg,$rs,$title);
//计算标题数量
$count=count($title[0]);
echo $count."<br>";
//通过标题数量进行内容采集
for ($i=0;$i<$count;$i++){ 

//设置内容页地址
$pr='/<a\s+href=\"[^>]+\">/isU';
preg_match_all($pr,$title[0][$i],$jurl);
$substr=substr($jurl[0][0],9);
$curl=substr($substr,0,-18);
//获取内容页代码
$c=file_get_contents($curl);
//设置内容页匹配正则
$pc='/<a\s+href=\"[^>]+\">/i';
//进行正则匹配搜索
preg_match($pc,$c,$content);
//输出标题
echo $title[0][$i]."<br>";
echo $title[1][$i]."<br>";
$concount=count($content[0]);
echo $concount."<br>";
echo $content[0][0];
for ($j=0;$j<$concount;$j++){ 

}
}
?>


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值