<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-167  評論-65  文章-0  trackbacks-0
    ruby 1.8.7 + rails 2.1.0

    打開 http://www.google.cn/finance?q=600001 這個網址 可以看到 谷歌財經的 右側 有個新聞區。。。這個新聞區就是從別的地方抓取來的
    截圖:


    現在我們也來仿照它來實現一個,首先rails解析rss有兩種方式,一種是用封裝好的類庫,一種是用原始的解析xml的方式,或者利用別人封裝好的庫 例如feedtools, rubyrss 等
    用類庫的方法:
        require 'rss/2.0'
        require 'open-uri'
        url = "http://news.google.cn/news?pz=1&ned=ccn&hl=zh-CN&topic=b&output=rss"
        @feed = RSS::Parser.parse(open(url).read, false)
        @feed.items.each do |item|
          puts item.title
          puts item.link
          puts  item.description
        end


    解析xml的方法:
    在lib下建立一個RssParser的類,這樣在任何地方都可以調用
    class RssParser
      require 'rexml/document'
      def self.run(url)
        xml = REXML::Document.new Net::HTTP.get(URI.parse(url))
        data = {
          :title    => xml.root.elements['channel/title'].text,
          :home_url => xml.root.elements['channel/link'].text,
          :rss_url  => url,
          :items    => []
        }
        xml.elements.each '//item' do |item|
          new_items = {} and item.elements.each do |e|
            new_items[e.name.gsub(/^dc:(\w)/,"\1").to_sym] = e.text
          end
          data[:items] << new_items
        end
        data
      end
    end


    action中使用:
      def test
        feed = RssParser.run("http://news.google.cn/news?pz=1&ned=ccn&hl=zh-CN&topic=b&output=rss")
        feed1 = feed[:items][0]
        feed2 = feed[:items][0]
        feed3 = feed[:items][0]
        # combine the feeds into an array
        @feeds = [feed1, feed2, feed3]
        # parse the pubDate strings into a DateTime object
        @feeds.each {|x| x[:pubDate] = DateTime.parse(x[:pubDate].to_s)}
        # iterate through each feed, sorting by pubDate
        @feeds.sort! {|a,b| a[:pubDate] <=> b[:pubDate]}
        # reverse the array to sort by descending pubDate
        @feeds.reverse!
        @feeds.each do |feed|
          puts feed[:title]
          puts feed[:link]
          puts feed[:pubDate]
        end
      end


    那么上面的title link description 是從哪里來的呢。。。這個是rss2.0的xml結構,一般情況下是這樣的:
    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
    <channel>
      <title>Example Feed</title>
    <description>Insert witty or insightful remark here</description>
    <link>http://example.org/</link>
    <lastBuildDate>Sat, 13 Dec 2003 18:30:02 GMT</lastBuildDate>
    <managingEditor>johndoe@example.com (John Doe)</managingEditor>
    <item>
    <title>Atom-Powered Robots Run Amok</title>
    <link>http://example.org/2003/12/13/atom03</link>
    <guid isPermaLink="false">urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</guid>
    <pubDate>Sat, 13 Dec 2003 18:30:02 GMT</pubDate>
    <description>Some text.</description>
    </item>
    </channel>
    </rss>

    或者你可以查看rss的頁面源代碼,或者puts下  @feed = RSS::Parser.parse(open(url).read, false)的結果都可以看到上面的這中xml文檔結構

    好,下面我們開始實現上面圖的新聞:
    我們可以把這個部分放在partial里,所以只需要helper和partial文件
    helper:
    def feed_collection(param)
    require 'rss/2.0'
    require 'open-uri'
    # from news.google.cn
    urlhot = "http://news.google.cn/news?pz=1&ned=ccn&hl=zh-CN&topic=b&output=rss"
    urlfinance = "http://news.google.cn/news?pz=1&ned=ccn&hl=zh-CN&topic=ecn&output=rss"
    urlfund = "http://news.google.cn/news?pz=1&ned=ccn&hl=zh-CN&topic=stc&output=rss"
    urlfinancing = "http://news.google.cn/news?pz=1&ned=ccn&hl=zh-CN&topic=pf&output=rss"
    case param
    when 'hot'
    RSS::Parser.parse(open(urlhot).read, false)
    when 'finance'
    RSS::Parser.parse(open(urlfinance).read, false)
    when 'fund'
    RSS::Parser.parse(open(urlfund).read, false)
    when 'financing'
    RSS::Parser.parse(open(urlfinancing).read, false)
    end
    end

    def feed_link(param)
    require 'cgi'
    CGI.unescape(param.slice(/(http%).*(&)/)).gsub(/&/,'') if param # 把十六進制路徑 例如http%3A2F之類的轉化為 字符
    end

    def feed_title(param)
    param.slice(/.*(-)/).gsub(/-/,"") if param #截取需要的title
    end

    def feed_from(param)
    param.slice(/( - ).*/).from(2) if param # 截取需要的部分
    end


    partial: _feednews.erb.html
    <div class="slides">
    <div><%= render :partial => 'shared/feednews_item',:collection => feed_collection("hot").items %></div>
    <div><%= render :partial => 'shared/feednews_item',:collection => feed_collection('finance').items %></div>
    <div><%= render :partial => 'shared/feednews_item',:collection => feed_collection('fund').items %></div>
    <div><%= render :partial => 'shared/feednews_item',:collection => feed_collection('financing').items %></div>
    </div>

    主義這里參考了 jquery的loopslider 插件(幻燈片) 加載顯示的只是第一個div部分,可以參考:
    http://github.com/nathansearles/loopedSlider/tree/master

    partial: _feednews_item.html.erb
    <ul>
    <% unless feednews_item.nil? %>
    <li class="news"><a href="<%= feed_link(feednews_item.link) %>" target="_blank"><%= feed_title(feednews_item.title) %></a>

    <span class="grey small"><span> <%= feed_from(feednews_item.title) %></span>&nbsp;&mdash;&nbsp;<span><%= feednews_item.pubDate.to_date %></span></span></li>
    <% end %>
    </ul>

    okay....已經成功了,我實現的截圖:


    ref:
    http://www.rubycentral.com/book/ref_c_string.html
    http://www.javaeye.com/topic/60620
    http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Regular_Expressions
    http://paranimage.com/15-jquery-slideshow-plugins/#respond
    http://hi.baidu.com/todayz/blog/item/83c1b219d966fd4142a9ad5f.html
    http://dennis-zane.javaeye.com/blog/57538
    http://sporkmonger.com/projects/feedtools/
    http://rubyrss.com/
    http://rubyrss.com/
    http://www.superwick.com/archives/2007/06/09/rss-feed-parsing-in-ruby-on-rails/
    http://www.ruby-forum.com/topic/144447




    write by feng
    posted on 2009-08-18 10:45 fl1429 閱讀(807) 評論(0)  編輯  收藏 所屬分類: Rails
    已訪問數:
    free counters
    主站蜘蛛池模板: 亚洲午夜精品一区二区公牛电影院| 亚洲 综合 国产 欧洲 丝袜| 亚洲91av视频| 男人都懂www深夜免费网站| 国产综合精品久久亚洲| 久久久受www免费人成| 亚洲自偷自偷图片| 中国一级特黄的片子免费| 亚洲精品成人无码中文毛片不卡| 99精品视频免费| 亚洲高清在线观看| 精品无码无人网站免费视频 | 亚洲成在人天堂在线| 91精品成人免费国产| 亚洲人成在线影院| 亚洲大片免费观看| 国产亚洲精品成人AA片| 午夜精品在线免费观看| 色爽黄1000部免费软件下载| 亚洲人成网7777777国产| 99精品视频免费观看| 亚洲高清乱码午夜电影网| 久久精品亚洲福利| 国产精品99久久免费观看| 亚洲综合色区中文字幕| 亚洲成av人片在线观看天堂无码| 中文在线观看免费网站| 亚洲成aⅴ人在线观看| 免费无码黄网站在线观看| 国产免费久久精品丫丫| 久久亚洲熟女cc98cm| 四虎www成人影院免费观看| eeuss影院www天堂免费| 亚洲导航深夜福利| 男人的天堂亚洲一区二区三区| 免费看美女午夜大片| 精品无码一区二区三区亚洲桃色 | 国产黄片不卡免费| 亚洲国产成人综合| 亚洲中文字幕伊人久久无码| 69av免费观看|