0
1

More than 5 years have passed since last update.

chintai.netで家賃相場を調べるrubyプログラム

Last updated at Posted at 2017-07-24

Qiitaとrubyと家賃相場調査初心者です!

いまいちQiita使い方分からないんですが
http://qiita.com/taram
さんのコメントを元に
修正と学習の跡を載せた_lesso.rb を載せて更新しました
1000.times{ puts "コメント感謝です!!!!" }
ruby1.8環境で少なくともscan部分は動いていたけど、真相は謎...

手元の環境で試せてないから16行目と17行目が動くか微妙
open-uriも微妙
scanとgsub!部分はもっと簡潔に書けるはずだけど書き方分からない
32~37行目はもっとスマートに書けるけどはずだけど分からなかった

41行目以降のCSV書き込みコメントアウト外して動いた

オブジェクト指向で書きたいけどイマイチ勝手が分からないのでそのうち覚えたい

souba_lesson.rb
#souba_lesson.rb

require 'open-uri'
require "pp"
require "csv"

data = []
paired_data = []

sio = OpenURI.open_uri('https://www.chintai.net/tokyo/ensen/101001/rent/')
#txt = sio.read

pp sio #fileオブジェクト

#                           <p class="name"><a href="/tokyo/ensen/000000065/rent/">目黒</a></p>
#                                   <span class="js_graph_data">10.30</span>万円

#IO.foreach("./ https://www.chintai.net/tokyo/ensen/101001/rent/ のhtmファイル") do |l|
#IO.foreach(txt) do |l|

sio.each_line do |l| #fileオブジェクトのeach_lineメソッドでイテレートしつつlというstringオブジェクトをそれぞれ作る
#pp l
    if l =~ %r|p class="name"|
#        station = l.scan(%r|rent/">\D+</a|).to_s
#       station.gsub!(%r|rent/">|, "")
#        station.gsub!(%r|rent/">¥D+<|)
#       station = l.slice(%r|r(e)nt/">(.+)<(/a)|, 2) #sliceの第二引数は()で閉じた箇所を指定して抜き出せる
        station = l.slice(%r|rent/">(.+)</a|, 1)
        data << station
    end
    if l =~ %r|js_graph_data|
#        rent = l.scan(%r|data">[0-9]+\.[0-9]+</span>|).to_s
#       rent.gsub!(%r|data">|, "")
#        rent.gsub!(%r|</span>|, "")
#       rent = l.slice(%r|"js_graph_data">(\d+\.\d+)</span>|, 1) #[0-9]より\dがベター?かも
        rent = l.slice(%r|"js_graph_data">([0-9]+\.[0-9]+)</span>|, 1) #個人的には[0-9]の方が好き
        data << rent.to_f
    end
end

=begin
#while data != []
until data.empty? #の方が読みやすい
    pair = []
    pair << data.shift
    pair << data.shift
    paired_data << pair
end
=end

"abcde".each_char do |s| p s end
#.each_charやeach_sliceはEnumeratorクラスのメソッド

pp (1..9).each_slice(3).to_a #each_slice便利
#参考 http://qiita.com/QUANON/items/749f4a2a79dafdaff57f

paired_data = data.each_slice(2).to_a
#dataというarrayオブジェクトをeach_slice(2)で整形されたEnumeratorオブジェクトにしてEnumeratorメソッドの.to_aでarrayオブジェクトに戻している

pp paired_data

CSV.open('souba_result.csv','w') do |c|
   paired_data.each do |a|
       c << a
   end
end
souba.rb
#souba.rb

require 'open-uri'
require "pp"
require "csv"

data = []
paired_data = []

sio = OpenURI.open_uri('https://www.chintai.net/tokyo/ensen/101001/rent/')
txt = sio.read

#                           <p class="name"><a href="/tokyo/ensen/000000065/rent/">目黒</a></p>
#                                   <span class="js_graph_data">10.30</span>万円

#IO.foreach("./ https://www.chintai.net/tokyo/ensen/101001/rent/ のhtmファイル") do |l|
IO.foreach(txt) do |l|
    if l =~ %r|p class="name"|
        station = l.scan(%r|rent/">\D+</a|).to_s
        station.gsub!(%r|rent/">|, "")
        station.gsub!(%r|</a|, "")
        data << station
    end
    if l =~ %r|js_graph_data|
        rent = l.scan(%r|data">[0-9]+\.[0-9]+</span>|).to_s
        rent.gsub!(%r|data">|, "")
        rent.gsub!(%r|</span>|, "")
        data << rent.to_f
    end
end

while data != []
    pair = []
    pair << data.shift
    pair << data.shift
    paired_data << pair
end

pp paired_data

#CSV.open('souba_result.csv','w') do |c|
#   paired_data.each do |a|
#       c << a
#   end
#end
0
1
2

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
0
1