LoginSignup
7
9

More than 5 years have passed since last update.

不動産取引価格情報取得APIで坪単価を調べるrubyプログラム

Last updated at Posted at 2017-07-26

qiitaとrubyとapiと不動産取引価格調査初心者です!!

※ruby2.0では動作しないです。Jsonクラスのdigメソッドでコケました。どうやらruby2.3から追加されたみたいです。Jsonの取り扱いの記事はもっと色々読みたいです。
※手元のmac環境がruby2.0だったためrbenvでmacのrubyを最新にするを参照してrubyアップデートしました!多謝!

日本の行政機関等が公開しているAPIについてのまとめ(2016年8月17日暫定版。随時更新) を読んでapi試してみたいと思って少し前に書いたコード(土地の値段を調べるのも興味ある)

apiの説明はこちら

例のごとく、このコード書いてしばらく経った今何をどう書いていたかきちんと思い出せない...

area_price.rb
#area_price.rb

require 'net/http'
require 'uri'
require 'json'

url = "http://www.land.mlit.go.jp/webland/api/TradeListSearch?from=20101&to=20161&city=13421"

uri = URI.parse(url)
json = Net::HTTP.get(uri)
result = JSON.parse(json)

trade_data = []
(0..100).each do |i|
    if result.dig("data", i, "UnitPrice").to_i <= 2000
        trade_data << result.dig("data", i)
    end
end

json_str = JSON.pretty_generate(trade_data)

puts json_str
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9