美文网首页
查看nba排名的爬虫程序

查看nba排名的爬虫程序

作者: kamionayuki | 来源:发表于2015-05-26 20:24 被阅读118次

rank.rb

#encoding:utf-8
require 'nokogiri'
require 'open-uri'

def get_rank(param="")
    url = param == "-d" ? "http://g.hupu.com/nba/standings/division" : "http://g.hupu.com/nba/standing"
    result = ""

    page = Nokogiri::HTML(open(url))

    table = page.css("table")
    trs = table.css("tr")
    trs.each do |tr|
        tds = tr.css("td")
        tds.each_with_index do |td,i|
            temp = td.text.gsub(/[\n| ]+/,"")
            if i == 1
                result << "%-15s" % [temp]
            else
                result << temp
            end
            result << "\t"
        end
        result << "\n"
    end

    result
end

if $*[0] == "-d" || $*[0] == nil
    puts "开始获取数据......"

    puts get_rank($*[0])
else
    puts "wrong args."
end

Usage:
ruby rank.rb or ruby rank.rb -d

相关文章

网友评论

      本文标题:查看nba排名的爬虫程序

      本文链接:https://www.haomeiwen.com/subject/pzerqttx.html