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