ruby版的抓基金代码
#!/usr/bin/env ruby
# Time-stamp: <2007-08-02[星期四] 16:01:19 dongsheng>
require 'net/http'
require 'iconv'
url = URI.parse('http://my.fund.163.com/stock/rankkfs.htm')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
content = res.body
#content = Iconv.iconv('utf-8', 'gb2312', content)
#puts content
re_items = /\<tr align="center" bgcolor="(#EFEFEF|#E7F3FE)" class="bzi">(.*?)\<\/tr\>/im
re_anchor = /\<td\>\<a href=.*?\>(.*?)\<\/a\>\<\/td\>/im
re_normal = /\<td\>([0-9-]*\.*\d*)\<\/td\>/im
content.scan(re_items) do |x,y|
anchors = y.scan(re_anchor)
puts "code: #{Iconv.iconv('utf-8', 'gb2312', anchors[0].to_s)}"
puts "name: #{Iconv.iconv('utf-8', 'gb2312', anchors[1].to_s)}"
puts "company: #{Iconv.iconv('utf-8', 'gb2312', anchors[2].to_s)}"
normal = y.scan(re_normal)
puts "date: #{normal[0]}"
puts "util: #{normal[1]}"
puts "total: #{normal[2]}"
puts "rate: #{normal[3]}"
end
# Time-stamp: <2007-08-02[星期四] 16:01:19 dongsheng>
require 'net/http'
require 'iconv'
url = URI.parse('http://my.fund.163.com/stock/rankkfs.htm')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
content = res.body
#content = Iconv.iconv('utf-8', 'gb2312', content)
#puts content
re_items = /\<tr align="center" bgcolor="(#EFEFEF|#E7F3FE)" class="bzi">(.*?)\<\/tr\>/im
re_anchor = /\<td\>\<a href=.*?\>(.*?)\<\/a\>\<\/td\>/im
re_normal = /\<td\>([0-9-]*\.*\d*)\<\/td\>/im
content.scan(re_items) do |x,y|
anchors = y.scan(re_anchor)
puts "code: #{Iconv.iconv('utf-8', 'gb2312', anchors[0].to_s)}"
puts "name: #{Iconv.iconv('utf-8', 'gb2312', anchors[1].to_s)}"
puts "company: #{Iconv.iconv('utf-8', 'gb2312', anchors[2].to_s)}"
normal = y.scan(re_normal)
puts "date: #{normal[0]}"
puts "util: #{normal[1]}"
puts "total: #{normal[2]}"
puts "rate: #{normal[3]}"
end
ruby版同样有字符编码的问题, 所以, 使用了Iconv来作转码.
python版见: http://www.cnblogs.com/angelface/archive/2007/08/03/840924.html