您的位置:首页 > 其它

file_get_contents进行页面采集数据

2016-09-24 11:44 309 查看
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: