PHP培训作业1:新闻采集,显示列表和内容

时间:12-04-22 栏目:网站开发 作者:admin 评论:0 点击: 8,693 次

作业A:新闻采集,显示列表和内容

要求:

采集:http://tech.qq.com/l/web/webnews/webnews.htm 页面内容

列表效果:http://localhost/news/index.php
=======================

·亚马逊商品现标价乌龙 取消订单涉嫌欺诈 04月21日 16:48
·民诉法修正草案将电子数据列入证据惹争议 04月21日 14:39
·百思买任命委员会寻找新CEO 04月21日 14:29

点击新闻打开效果:
=======================
http://localhost/news/index.php?news=/a/20120421/000154.htm

标题:亚马逊商品现标价乌龙 取消订单涉嫌欺诈
时间:2012年04月21日16:48

正文:
据经济之声《天天315》报道,湖北武汉的消费者陈先生最近向我们反映,前不久,他发现亚马逊网站上一款手机标价两千多元,但下单后成交价格只有一千多。本来他很高兴,可是之后发生的事情却让他很生气。

参考代码:

<?php
/**
* 作业A:新闻采集,显示列表和内容
*/

if(!isset($_GET['news'])){
$main_url = 'http://tech.qq.com/l/web/webnews/webnews.htm';
$main_content = file_get_contents($main_url);

$c = explode('<div class="mod newslist">',$main_content);
$c = explode('</div>',$c[1]);
$content = $c[0];

preg_match_all('@href="([^"]+)">([^<]+)<@',$content,$match);

if($match){
foreach($match[2] as $k=>$title){
$url=$match[1][$k];
$url=str_replace('http://tech.qq.com','',$url);
echo '<a href="?news='.urlencode($url).'" target=_blank>'.$title.'</a>';
echo "<br>\r\n";

}
}

}else{
if($_GET['news']){
$newsurl = $_GET['news'];
$newsurl = 'http://tech.qq.com'.$newsurl;
$new_content = file_get_contents($newsurl);

preg_match('@<h1>([^<]+)</h1>@',$new_content,$match);
$title = $match[1];
preg_match('@<span class="pubTime">([^<]+)</span>@',$new_content,$match);
$date = $match[1];
preg_match('@<div id="Cnt-Main-Article-QQ" bossZone="content">(.*)</div>@',$new_content,$match);
$content = $match[1];

echo '<br><strong>标题:</strong>';
echo $title;
echo '<br><strong>时间:</strong>';
echo $date;
echo '<br><strong>内容:</strong><br>';
echo $content;
}

}
?>

声明: 本文由( admin )原创编译,转载请保留链接: PHP培训作业1:新闻采集,显示列表和内容

PHP培训作业1:新闻采集,显示列表和内容:等您坐沙发呢!

发表评论


------====== 本站公告 ======------
联系信息:
电话:19970108113(微信同号) QQ:3142401606
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。

读者排行