首先要先感謝台中市喀哩國小資訊老師提供的原始程式碼,這樣讓我節省很多時間,不用再去開發網頁爬蟲程式,這裡就按照原始程式碼修改為兩個樣式!直接可以顯示臺中市教育局校園活動新聞內容
首先區要知道所屬學校的網址,這樣程式才能去爬行撈資料,查詢學校網址的方法如下:
1、先進入臺中市校園活動新聞首頁
http://www.tc.edu.tw/SchoolNews/show
2、找到右下角的學校發表排行區塊
3、找到自己學校點進去,上面的網址就是該學校活動新聞網址,之後的程式需要貼入
樣式一:區塊輪播樣式
//PHP程式碼
$show_num=1;//顯示幾則新聞,大於1則會有輪播效果
//取得新聞
$url = "http://www.tc.edu.tw/SchoolNews/show/school/id/546"; //貼上校園活動新聞網址
$contents = addslashes(file_get_contents($url));
$contents = str_replace (addslashes('href="/'), addslashes(' href="http://www.tc.edu.tw/'), $contents);
$contents = str_replace (addslashes('src="/'), addslashes(' src="http://www.tc.edu.tw/'), $contents);
$new_arr=explode(addslashes('<div class="articles" style="margin-bottom: 10px;">'),$contents);
$i=1;
while($i<=$show_num){
$new_arr[$i]=stripslashes($new_arr[$i]);
//取得標題
$title=str_replace("<a","<a target=_blank ",CatchStr($new_arr[$i],"<a","a>"));
$title=str_replace("~","<br>",$title);
$t_url=substr(CatchStr($title,"h",">"),0,-1);
$news_id=substr($t_url,51,-1);
//處理圖片
$img_src=substr(CatchStr($new_arr[$i],"src","alt"),5,-5);
$img_size=getimagesize($img_src);
$img_w=241;
$img_h=auto;
$image="<a target='_blank' ".$t_url."><img class='thumbnail' width={$img_w} height={$img_h} src='{$img_src}' alt='' /></a>";
//處理報導者
$report=CatchStr($new_arr[$i],"<p","p>");
$r_date=substr($report,18,10);
$reporter=substr(CatchStr($report,"a>",","),2,-1);
echo "<div class = 'school_news'></$show_num)>".$image."<small>".$title."</small></div>";
$i++;
}
function CatchStr($Str, $StaKey, $EndKey){
$sp=strpos($Str,$StaKey);
$ep=strpos($Str,$EndKey);
$sl=strlen($StaKey);
$el=strlen($EndKey);
$res=substr($Str,$sp,$ep-$sp+$el);
return $res;
}
//輪播範圍終點
echo "</div>";
PS:複製以上的程式碼貼到Xoops自訂區塊中,下面的內容類型須設為:PHP腳本
//CSS樣式
#school_news .thumbnail{
position: relative;
margin: 0px 0px 5px 0px;
}
.school_news{
line-height: 160%;
font-size: 110%;
}
.school_news a{
position: relative;
color: #393939
}
#school_newslistbox{
position: relative;
padding: 0px 0px 10px 0px;
}
#school_newslistbox li:nth-child(odd)
{
position: relative;
height: 49px;
margin: 0px 0px 0px 0px;
padding: 5px 0px 0px 44px;
font-size: 125%;
line-height: 170%;
overflow: hidden;
background: url(../../img/modules/tadnews/dppstc_66.png) no-repeat left 2px,url(../../img/modules/tadnews/dppstc_63.png) no-repeat left bottom;
}
#school_newslistbox li:nth-child(even)
{
position: relative;
height: 49px;
margin: 0px 0px 0px 0px;
padding: 5px 0px 0px 44px;
font-size: 125%;
line-height: 170%;
overflow: hidden;
background: url(../../img/modules/tadnews/dppstc_67.png) no-repeat left 2px,url(../../img/modules/tadnews/dppstc_63.png) no-repeat left bottom;
}
.school_newslast a{
color: #000000;
}
.school_newslast a:hover{
color: #789314;
}
#r_date{
padding: 1px 5px;
margin: 0px 2px 0px 0px;
background: #C84297 url(../../img/modules/tadnews/) no-repeat left 6px;
border-radius: 10px;
color: #ffffff;
}
首先區要知道所屬學校的網址,這樣程式才能去爬行撈資料,查詢學校網址的方法如下:
1、先進入臺中市校園活動新聞首頁
http://www.tc.edu.tw/SchoolNews/show
2、找到右下角的學校發表排行區塊
3、找到自己學校點進去,上面的網址就是該學校活動新聞網址,之後的程式需要貼入
樣式一:區塊輪播樣式
//PHP程式碼
echo "<script type='text/javascript' src='http://www.tc.edu.tw/js/jquery-1.6.4.min.js'></script>
<script type='text/javascript' src='http://www.tc.edu.tw/js/plugins/jquery.cycle.all.min.js'></script>
<script type='text/javascript'>
$('#school_news').cycle({
fx: 'turnDown',
delay: -2000,
speed: 2500 ,
timeout: 5000 ,
pause:1
});
</script>
$show_num=1;//顯示幾則新聞,大於1則會有輪播效果
//取得新聞
$url = "http://www.tc.edu.tw/SchoolNews/show/school/id/546"; //貼上校園活動新聞網址
$contents = addslashes(file_get_contents($url));
$contents = str_replace (addslashes('href="/'), addslashes(' href="http://www.tc.edu.tw/'), $contents);
$contents = str_replace (addslashes('src="/'), addslashes(' src="http://www.tc.edu.tw/'), $contents);
$new_arr=explode(addslashes('<div class="articles" style="margin-bottom: 10px;">'),$contents);
$i=1;
while($i<=$show_num){
$new_arr[$i]=stripslashes($new_arr[$i]);
//取得標題
$title=str_replace("<a","<a target=_blank ",CatchStr($new_arr[$i],"<a","a>"));
$title=str_replace("~","<br>",$title);
$t_url=substr(CatchStr($title,"h",">"),0,-1);
$news_id=substr($t_url,51,-1);
//處理圖片
$img_src=substr(CatchStr($new_arr[$i],"src","alt"),5,-5);
$img_size=getimagesize($img_src);
$img_w=241;
$img_h=auto;
$image="<a target='_blank' ".$t_url."><img class='thumbnail' width={$img_w} height={$img_h} src='{$img_src}' alt='' /></a>";
//處理報導者
$report=CatchStr($new_arr[$i],"<p","p>");
$r_date=substr($report,18,10);
$reporter=substr(CatchStr($report,"a>",","),2,-1);
echo "<div class = 'school_news'></$show_num)>".$image."<small>".$title."</small></div>";
$i++;
}
function CatchStr($Str, $StaKey, $EndKey){
$sp=strpos($Str,$StaKey);
$ep=strpos($Str,$EndKey);
$sl=strlen($StaKey);
$el=strlen($EndKey);
$res=substr($Str,$sp,$ep-$sp+$el);
return $res;
}
//輪播範圍終點
echo "</div>";
PS:複製以上的程式碼貼到Xoops自訂區塊中,下面的內容類型須設為:PHP腳本
//CSS樣式
position: relative;
margin: 0px 0px 5px 0px;
}
.school_news{
line-height: 160%;
font-size: 110%;
}
.school_news a{
position: relative;
color: #393939
}
樣式二:條列式樣式
//PHP程式碼
echo "<div id= 'school_newslistbox' ><ul>";
$show_num=7;//顯示幾則新聞
//取得新聞
$url = "http://www.tc.edu.tw/SchoolNews/show/school/id/546"; //貼上校園活動新聞網址
$contents = addslashes(file_get_contents($url));
$contents = str_replace (addslashes('href="/'), addslashes(' href="http://www.tc.edu.tw/'), $contents);
$contents = str_replace (addslashes('src="/'), addslashes(' src="http://www.tc.edu.tw/'), $contents);
$new_arr=explode(addslashes('<div class="articles" style="margin-bottom: 10px;">'),$contents);
$i=1;
while($i<=$show_num){
$new_arr[$i]=stripslashes($new_arr[$i]);
//取得標題
$title=str_replace("<a","<a target=_blank ",CatchStr2($new_arr[$i],"<a","a>"));
$title=str_replace("~","<br>",$title);
$t_url=substr(CatchStr2($title,"h",">"),0,-1);
$news_id=substr($t_url,51,-1);
//處理報導者
$report=CatchStr2($new_arr[$i],"<p","p>");
$r_date=substr($report,18,10);
$reporter=substr(CatchStr2($report,"a>",","),2,-1);
echo "<li><div class='school_newslast'> <span id='r_date'>".$r_date."</span>".$title."</div></li>";
$i++;
}
function CatchStr2($Str, $StaKey, $EndKey){
$sp=strpos($Str,$StaKey);
$ep=strpos($Str,$EndKey);
$sl=strlen($StaKey);
$el=strlen($EndKey);
$res=substr($Str,$sp,$ep-$sp+$el);
return $res;
}
//輪播範圍終點
echo "</ul></div>
";
PS:複製以上的程式碼貼到Xoops自訂區塊中,下面的內容類型須設為:PHP腳本
//PHP程式碼
echo "<div id= 'school_newslistbox' ><ul>";
$show_num=7;//顯示幾則新聞
//取得新聞
$url = "http://www.tc.edu.tw/SchoolNews/show/school/id/546"; //貼上校園活動新聞網址
$contents = addslashes(file_get_contents($url));
$contents = str_replace (addslashes('href="/'), addslashes(' href="http://www.tc.edu.tw/'), $contents);
$contents = str_replace (addslashes('src="/'), addslashes(' src="http://www.tc.edu.tw/'), $contents);
$new_arr=explode(addslashes('<div class="articles" style="margin-bottom: 10px;">'),$contents);
$i=1;
while($i<=$show_num){
$new_arr[$i]=stripslashes($new_arr[$i]);
//取得標題
$title=str_replace("<a","<a target=_blank ",CatchStr2($new_arr[$i],"<a","a>"));
$title=str_replace("~","<br>",$title);
$t_url=substr(CatchStr2($title,"h",">"),0,-1);
$news_id=substr($t_url,51,-1);
//處理報導者
$report=CatchStr2($new_arr[$i],"<p","p>");
$r_date=substr($report,18,10);
$reporter=substr(CatchStr2($report,"a>",","),2,-1);
echo "<li><div class='school_newslast'> <span id='r_date'>".$r_date."</span>".$title."</div></li>";
$i++;
}
function CatchStr2($Str, $StaKey, $EndKey){
$sp=strpos($Str,$StaKey);
$ep=strpos($Str,$EndKey);
$sl=strlen($StaKey);
$el=strlen($EndKey);
$res=substr($Str,$sp,$ep-$sp+$el);
return $res;
}
//輪播範圍終點
echo "</ul></div>
";
PS:複製以上的程式碼貼到Xoops自訂區塊中,下面的內容類型須設為:PHP腳本
//CSS樣式
position: relative;
padding: 0px 0px 10px 0px;
}
#school_newslistbox li:nth-child(odd)
{
position: relative;
height: 49px;
margin: 0px 0px 0px 0px;
padding: 5px 0px 0px 44px;
font-size: 125%;
line-height: 170%;
overflow: hidden;
background: url(../../img/modules/tadnews/dppstc_66.png) no-repeat left 2px,url(../../img/modules/tadnews/dppstc_63.png) no-repeat left bottom;
}
#school_newslistbox li:nth-child(even)
{
position: relative;
height: 49px;
margin: 0px 0px 0px 0px;
padding: 5px 0px 0px 44px;
font-size: 125%;
line-height: 170%;
overflow: hidden;
background: url(../../img/modules/tadnews/dppstc_67.png) no-repeat left 2px,url(../../img/modules/tadnews/dppstc_63.png) no-repeat left bottom;
}
.school_newslast a{
color: #000000;
}
.school_newslast a:hover{
color: #789314;
}
#r_date{
padding: 1px 5px;
margin: 0px 2px 0px 0px;
background: #C84297 url(../../img/modules/tadnews/) no-repeat left 6px;
border-radius: 10px;
color: #ffffff;
}
CSS樣式及新聞顯示筆數可自行調整,這兩個區塊功能經測試功能都沒問題的,歡迎有需要的臺中學校單位自行取用。
教學撰寫:徐嘉裕
留言
張貼留言