汇率
上证,深证等
代码共享(ruby)
# coding: utf-8
require 'nokogiri'
require 'csv'
require 'net/http'
FXs = [
'JPY',
'AUD',
'XAU',
'EUR',
'GBP'
]
GUs = [
'sh000001',
'sz002344',
'sz300159',
'sh600717',
'sh600332'
]
results = []
http = Net::HTTP.new("hq.sinajs.cn")
GUs.each do |gu|
response,body = http.get("/list=#{gu}")
doc = response.body
re = doc.scan(/.*\"(.*)\";/)
array = CSV.parse(re[0][0])
results << "#{gu}: #{array[0][3]}"
end
http2 = Net::HTTP.new('finance.yahoo.com')
response = http2.get('/webservice/v1/symbols/allcurrencies/quote')
xml = response.body
doc = Nokogiri::Slop(xml)
doc.list.resources.resource.each do |e|
FXs.each do |f|
fx = f + "=X"
if e.xpath('field[@name="symbol"]').text() == fx
price = e.xpath('field[@name="price"]').text()
p = fx == 'JPY=X' ? price : 1 / price.to_f
results << format("%s: %.4f\n", f, p.to_f)
end
end
end
results.each {|e| puts e}
我现在关注的股票都给你了 (^^)