先讓大家看看效果吧:
yahoo news : http://thefirstwind.ddo.jp/~devuser/cgi-bin/yahoonews.pl
csdn personal blog : http://thefirstwind.ddo.jp/~devuser/cgi-bin/csdnblog_thefirstwind.pl
1 首先是前期的準備
編程環境: RedHat Linux 9.0 perl v5.8.8
軟件包: LWP::Simple ,use XML::RSS;
爲了配好適合的軟件環境,可是花了一番的功夫,
因爲redhat linux 9.0 默認的perl版本下,安裝以上的module總是不成功,
最後仔細琢磨了一番,終於安裝好了兩個必要的module.大體步驟如下:
1.1安裝perl5.8.8
#wget http://search.cpan.org/CPAN/authors/id/N/NW/NWCLARK/perl-5.8.8.tar.gz
#tar -zxvf perl-5.8.8.tar.gz
然後進入perl-5.8.8的目錄,去讀INSTALL,安裝好新的perl
1.2安裝必要的module
#perl -MCPAN -e shell
初始化配置參考http://www.omakase.org/perl_module.htm
常用module安裝(可選)
cpan> install XML::XPath
cpan> install SOAP::Lite
cpan> install Jcode
cpan> install DB_File
cpan> install DBI
cpan> install DBD::mysql
cpan> install Bundle::LWP
cpan> install Bundle::CPAN
cpan> install CGI
cpan> install HTML::Entities
cpan> install Mail::Internet
cpan> install Pod::Text
cpan> install Bundle::XML
雖然可能在這裏面的操作中,會出現error但是,畢竟裝了不少,對以後安裝會有好處的。
本功能必備安裝(如果上面常用安裝已經執行過了,這一步,只是起到確認左右)
cpan> install XML::RSS
從新掛載cpan服務
cpan> reload cpan
2. 相關rss文件 和 perl 相關model 簡單介紹
2.1 rss文件
在這裏就是簡單的介紹一下主要的幾條語言,首先是要了解,幾個常用的大網站提供的rss的結構,
我就不多說了,自己看看就可以了,如果對於XML文檔結構不是很瞭解,去w3c school補補課吧。
這裏僅僅給出相關的幾個rss文件的地址,提供下載研究:
yahoo news : http://rss.news.yahoo.com/rss/topstories (經測試,本代碼中應用成功)
csdn personal blog : http://blog.csdn.net/thefirstwind/Rss.aspx (經測試,本代碼中應用成功)
2.2 LWP::Simple 和 XML::RSS 組合模塊代碼1
use XML::RSS;
my $url = 'http://blog.csdn.net/thefirstwind/Rss.aspx';
my $data_from_web = get($url);
my $rss = new XML::RSS;
$rss->parse($data_from_web);
2.3LWP::Simple 和 XML::RSS 組合模塊代碼2
use XML::RSS;
my $url = 'http://blog.csdn.net/thefirstwind/Rss.aspx';
my $file = './csdnblog_thefirstwind.rss';
mirror($url, $file);
my $rss = new XML::RSS;
$rss->parsefile($file);
以上兩種方法都可以,從現在開始$rss存有rss/xml文件的所有信息了,以後對其下面的節點,直接->{'節點名'},
如此引用就可以了。
3 代碼實例
好了說了那麼多廢話,終於要看看代碼了。
#邢曉寧版權所有,thefirstwind.ddo.jp,轉載請聲明出處
use strict;
use LWP::Simple;
use XML::RSS;
&html_top;
&html_body;
&html_end;
exit;
sub html_top{
printf "Content-type: text/html ";
printf "<!DOCTYPE HTML PUBLIC -//IETF//DTD HTML//EN> ";
printf <<___HTML_TOP;
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>cgi.test</title>
</head>
<body>
___HTML_TOP
}
sub html_end {
printf <<___HTML_END;
</body>
</html>
___HTML_END
}
sub html_body {
&call_readRss();
}
sub call_readRss {
# my $url = 'http://blog.csdn.net/thefirstwind/Rss.aspx';
my $url = 'http://rss.news.yahoo.com/rss/topstories';
my $file = './yahoonews.rss';
mirror($url, $file);
my $rss = new XML::RSS;
$rss->parsefile($file);
my $version = $rss->{'version'};
my $channel = $rss->{'channel'};
my $channel_title = $rss->{'channel'}->{'title'};
my $channel_copyright = $rss->{'channel'}->{'copyright'};
my $channel_link = $rss->{'channel'}->{'link'};
my $channel_description = $rss->{'channel'}->{'description'};
my $channel_language = $rss->{'channel'}->{'language'};
my $channel_lastBuildDate = $rss->{'channel'}->{'lastBuildDate'};
my $channel_ttl = $rss->{'channel'}->{'ttl'};
my $image = $rss->{'image'};
my $image_title = $rss->{'image'}->{'title'};
my $image_width = $rss->{'image'}->{'width'};
my $image_height = $rss->{'image'}->{'height'};
my $image_link = $rss->{'image'}->{'link'};
my $image_url = $rss->{'image'}->{'url'};
my $items_list = $rss->{'items'};
print "<a href="$channel_link" ><image border=0 src="$image_url" title="$channel_title"/></a> _fcksavedurl=""$image_url" title="$channel_title"/></a>" _fcksavedurl=""$image_url" title="$channel_title"/></a>" _fcksavedurl=""$image_url" title="$channel_title"/></a>" ";
print $channel_lastBuildDate,"<br> ";
print "<hr> ";
print "<table> ";
foreach my $item_hash (@$items_list){
print "<tr> ";
print "<th> ";
print "<a href="$$item_hash{'link'}">$$item_hash{'title'}</a>";
print "</th> ";
print "<td> ";
print $$item_hash{'pubDate'},"<br>";
print "</td> ";
print "</tr> <tr> ";
# print $$item_hash{'link'},"<br>";
print "<td colspan= 2 > ";
print $$item_hash{'description'},"<br>";
print "</td> ";
print "</tr> ";
print "<tr></tr> ";
}
print "</table> ";
print "<hr>$channel_copyright ";
關於代碼下載去
http://thefirstwind.ddo.jp/~devuser/cgi-bin/
rss文件是源rss的拷貝生成版本。
pl是執行代碼。
txt是用來在web上顯示的pl代碼。
最後一句廢話,版權所有,轉載請申明出處。