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>
相关文章推荐
- 数据采集之file_get_contents实现
- 解决php无法通过file_get_contents或curl采集页面内容
- file_get_contents获取一个页面并发送 POST 数据
- 解决php无法通过file_get_contents或curl采集页面内容
- 对于file_get_contents的到的 数据进行代码转换
- 解决PHP无法通过file_get_contents或curl采集页面内容
- 【转】通过file_get_contents来Post数据的实例 BY 膘叔
- 基于curl数据采集之单页面采集函数get_html的使用
- PHP利用Curl、socket、file_get_contents POST数据
- 解析file_get_contents模仿浏览器头(user_agent)获取数据
- 解析file_get_contents模仿浏览器头(user_agent)获取数据
- 基于curl数据采集之单页面采集函数get_html的使用
- 利用Curl、socket、file_get_contents POST数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- 基于curl数据采集之单页面并行采集函数get_htmls的使用
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- curl数据采集系列之多页面并行采集函数get_htmls
- PHP的file_get_contents获取远程页面乱码的问题
- 使用Curl、socket、file_get_contents三种方法POST提交数据