file_get_contents进行页面采集数据

<?php  
header("Content-Type:text/html;charset=utf-8");
$usr="http://sports.sohu.com/nba.shtml";
//设置响应时间为0
set_time_limit(0);


$str=file_get_contents($usr);
//进行页面转码
$str=iconv("GBK", "utf-8", $str);
//echo $str;
 $reg='#<div style="HEIGHT: 290px; OVERFLOW: hidden" id="columnID" class="heavyColumn">.*<div id="media-list" class="media-list clear">#isU';
 preg_match($reg,$str,$arr);
 $reg1='#<h4><a onFocus="undefined" title="" href=".*" target="_blank">(.*)</a></h4>#isU';
 preg_match_all($reg1,$arr[0],$title);
 $reg2='#<p>(.*)<a onFocus="undefined" href=".*" target="_blank">(.*)<div class="r">#isU';
 preg_match_all($reg2,$arr[0],$content);
 $reg3='#<img alt="NBA" src="(.*)" border="0" height="100" width="100"></a>#isU';
 preg_match_all($reg3,$arr[0],$image);
 foreach($image[1] as $key=>$val){
 	$sub=substr($val,strrpos($val,'.'));
 	$val=file_get_contents($val);
 	$imgname=rand(1000,9999).time().$sub;
 	file_put_contents($imgname,$val);
 	$image[2][$key]=$imgname;
 }
 $list=array();
 foreach($title[1] as $k=>$v){
   $list[$k]['title']=$v;
 }
 foreach($content[1] as $k=>$v){
   $list[$k]['content']=$v;
 }
 foreach($image[2] as $k=>$v){
   $list[$k]['img']=$v;
 }
 print_r($list);die;
 $dsn="mysql:host=localhost;dbname=seven7";
 $pdo=new PDO($dsn,'root','root');
 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
 $pdo->query('set names utf8');
 foreach($list as $k=>$v){
 	$title=$v['title'];
    $content=$v['content'];
 	$img=$v['img'];
 	$pdo->exec("INSERT INTO nba(title,content,img) VALUES('$title','$content','$img')");
 }
 $re=$pdo->query('SELECT * FROM nba');
 $re->setFetchMode(PDO::FETCH_ASSOC);
 $nbalist=$re->fetchAll();
//print_r($nbalist);
?>
<base href="./">
<table border="1">
<th>编号</th>
<th>标题</th>
<th>图片</th>
<th>内容</th>
<?php
foreach($nbalist as $k=>$v){;
?>
<tr>
<td><?php echo $v['id']  ;?></td>
<td><?php echo $v['title']  ;?></td>
<td><?php echo $v['content']  ;?></td>
<td><img src="<?php echo $v['img'] ;?>" ></td>
</tr>
<?php } ?>
</table>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值