<?php
/**
* 作业A:网易新闻采集,显示列表和内容
*/
header("Content-type:text/html;Charset=GBK;");
if(!isset($_GET['news'])){
$main_url = 'http://news.163.com/special/00013C0B/shizhengxw.html';
$main_content = file_get_contents($main_url);
$c = explode('<div class="right-1 right">',$main_content);
$c = explode('</div>',$c[1]);
$content = $c[0];
preg_match_all('/"http:\/\/news.163.com([^"]*)">([^<]*)<\/a><span>([^<]*)<\/span>/',$content,$match);
if($match){
foreach($match[2] as $k=>$title){
$time=$match[3][$k];
$url=$match[1][$k];
$url=str_replace('http://news.163.com','',$url);
echo '<a href="?news='.urlencode($url).'" target=_blank>'.$title.'</a>'.$time;
echo "<br>\r\n";
}
}
}else{
if($_GET['news']){
$newsurl = $_GET['news'];
$newsurl = 'http://news.163.com'.$newsurl;
$new_content = file_get_contents($newsurl);
preg_match('@<h1 id="h1title">([^<]+)</h1>@',$new_content,$match);
$title = $match[1];
$c= explode('<span class="info" style="display:block;">', $new_content);
$c= explode('<div class="wb-article-sharewraper wb-article-sharewraper-sm" id="wb-article-sharewraper-sm">', $c[1]);
$date=$c[0];
preg_match('@<div id="endText">(.*)</div>@s',$new_content,$match);
//preg_match('@<div id="Cnt-Main-Article-QQ" bossZone="content">(.*)</div>@s',$new_content,$match);
$contents = $match[1];
$content=explode('<span class="left">', $contents);
$content=$content[0];
echo '<br><strong>标题:</strong>';
echo $title;
echo '<br><strong>时间:</strong>';
echo $date;
echo '<br><strong>内容:</strong><br>';
echo $content;
}
}
?>