php抓取网页上的指定内容


<?php


 //ignore_user_abort(true);
 //set_time_limit(0);


 //获取所有的政府机关和事业单位
 $url = "http://www.tongda2000.com/company/news.php";
 $contents = file_get_contents($url);
 //如果出现中文乱码使用下面代码
 $getcontent = iconv("gb2312", "utf-8",$contents);
 echo $contents;
 //exit;
 
  $mode = "/ <ul class=\"newslist\">(.*)<div id=\"right\">/is";
 preg_match_all($mode,$contents,$matches);
 print_r($matches);
 //exit;
 $xinwen_str=$matches[0][0];
 //echo $xinwen_str;
 $xinwen_arr1=explode("<li>",$xinwen_str);
 
 foreach($xinwen_arr1 as $key=>$value){
  $xinwen_arr2=explode("</li>",$xinwen_arr1[$key]);
  $xinwen_arr1[$key]=$xinwen_arr2[0];
  $xinwen_arr1[$key]=str_replace("\n","",$xinwen_arr1[$key]);
  $xinwen_arr1[$key]=str_replace("\r","",$xinwen_arr1[$key]);
 }
//print_r($xinwen_arr1);
 
 for($j=1;$j<count($xinwen_arr1);$j++){
  $mode1 = "/(.*)<a/is";
 preg_match_all($mode1,$xinwen_arr1[$j],$matches1);
 $riqi_str=$matches1[0][0];
 $riqi_arr=explode(" ",$riqi_str);
 $riqi=$riqi_arr[0];//获取到日期
 echo $riqi."<br>";
 $mode2= "/>(.*)<\/a>/isU";
 preg_match_all($mode2,$xinwen_arr1[$j],$matches2);

 $title_str=$matches2[0][0];
 $title_arr=explode(">",$title_str);
 $title=str_replace("</a","",$title_arr[1]);//获取到名称
 echo $title."<br>";
 $lianjie_arr=explode("\"",$xinwen_arr1[$j]);
 $lianjie=$lianjie_arr[1];
   $a=strstr($lianjie,"http");
   if($a=="") $lianjie="http://www.tongda2000.com".$lianjie;
   echo $lianjie."<br>";
  }


?>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hai7425

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值