質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

0回答

946閲覧

suumo のデータをRubyでエクセルに項目ごとにダウンロードしたいです。プログラム修正依頼

tyoujobfideshom

総合スコア13

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2019/01/11 02:56

こんにちは
たびたび質問して、大変申し訳ありません。
下記は以前、友人に作っていただいたsuumoのデータをRUBYでダウンロードするプログラムですが、
suumoのソースコートが変わって、現在使えないのです。どのように修正したらいいのか、教えていただきたいです。どうぞよろしくお願い致します。

################################################

-- coding: utf-8 --

require 'rubygems'
require 'mechanize'
require 'kconv'

Setting

url = 'https://suumo.jp/jj/chintai/ichiran/FR301FC001/?ar=060&bs=040&ta=26&sc=26102&cb=0.0&ct=9999999&et=9999999&cn=9999999&mb=0&mt=9999999&shkr1=03&shkr2=03&shkr3=03&shkr4=03&fw2=&srch_navi=1'
#'https://suumo.jp/chintai/hyogo/sc_kobeshinada/'

#############################

AGENT_ALIASES = [
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36'
]

agent = Mechanize.new
agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
agent.cookie_jar.clear!
agent.follow_meta_refresh = true
agent.user_agent = AGENT_ALIASES[0]
agent.max_history = 2

i = 0
open("結果.csv", 'w'){|io| io.write("") }
begin
page = agent.get("#{url}pnz1#{i += 1}.html")
open('1.html', 'w'){|io| io.write(page.body) }

puts "#{i} Page" # 条件にあう物件がありません。条件を変更して再度検索してください。 break if !page.search('div.error_pop').empty? # 詳細を見るリンクを取集する result = [] page.search("#js-bukkenList").search('.cassetteitem').each do |div| info = {} info['賃貸種類'] = div.search('.ui-pct--util1').first.text info['物件名'] = div.search('.cassetteitem_content-title').first.text div.search('tbody').each do |tbody| info['階数'] = tbody.search('td')[2].text.strip info['賃料'] = tbody.search('td')[3].text.strip info['管理費'] = tbody.search('td')[4].text.strip temp = tbody.search('td')[5].text.strip info['敷礼'] = temp.split('/')[0] info['保証'] = temp.split('/')[1] info['敷引'] = temp.split('/')[2] info['償却'] = temp.split('/')[3] info['間取り'] = tbody.search('td')[6].text.strip info['専有面積'] = tbody.search('td')[7].text.strip div.search('td a').each do |link| next if link.text != '詳細を見る' info['url'] = link[:href] open('m_url.txt', 'w'){|io| io.write(info.to_s) } end result << info end end result.each_with_index do |e, i| if e['url'] =~ /^http/ page = agent.get(e['url']) open('html.txt', 'w'){|io| io.write(page.body) } else page = agent.get("https://suumo.jp#{e['url']}") open('2.html', 'w'){|io| io.write(page.body) } end puts page.uri.to_s open('url.txt', 'a'){|io| io.write(page.uri.to_s) } info = e info['住所'] = page.search('.detailinfo-col--04 div .detailvalue-txt').first.text.strip page.search('.detailnote-value/div').each_with_index do |div, index| next if (text = div.text.strip.split('/')).empty? case index when 0 info['①最寄駅線路名'] = text.first info['①最寄り駅名'] = text.last.split("\s")[0] info['①最寄り駅まで所要時間'] = text.last.split("\s")[1] when 1 info['②最寄駅線路名'] = text.first info['②最寄り駅名'] = text.last.split("\s")[0] info['②最寄り駅まで所要時間'] = text.last.split("\s")[1] when 2 info['③最寄駅線路名'] = text.first info['③最寄り駅名'] = text.last.split("\s")[0] info['③最寄り駅まで所要時間'] = text.last.split("\s")[1] when 3 info['④最寄駅線路名'] = text.first info['④最寄り駅名'] = text.last.split("\s")[0] info['④最寄り駅まで所要時間'] = text.last.split("\s")[1] end end tds = page.search('.table_gaiyou').search('td') page.search('.table_gaiyou').search('th').each_with_index do |th, index| if th.text.strip == '構造' info['構造'] = tds[index].text.strip elsif th.text.strip == '築年月' info['築年月'] = tds[index].text.strip elsif th.text.strip == '駐車場' temp = tds[index].text.strip.scan(/\d+/) info['駐車場まで距離'] = temp.first info['駐車場賃料'] = temp.last elsif th.text.strip == '間取り詳細' info['間取り詳細'] = tds[index].text.strip elsif th.text.strip == '階建' info['階建'] = tds[index].text.strip elsif th.text.strip == '条件' info['条件'] = tds[index].text.strip end end temp = page.search('#contents').search('.itemcassette') info['物件取扱店舗'] = temp.search('.itemcassette-header-ttl').first.text info['物件取扱店舗電話番号'] = temp.search('span.itemcassette_matrix-strong').first.text temp = temp.search('.itemcassette_matrix-cell02').first.text.strip.split('定休日:') info['物件取扱店舗営業時間'] = temp.first.strip info['物件取扱店舗定休日'] = temp.last.strip # open("#{i}.yaml", 'w'){|io| io.write(info.to_yaml) } res = "#{info['賃貸種類']},#{info['物件名']},#{info['住所']},#{info['①最寄駅線路名']},#{info['①最寄り駅名']},#{info['①最寄り駅まで所要時間']},#{info['②最寄駅線路名']},#{info['②最寄り駅名']},#{info['②最寄り駅まで所要時間']},#{info['③最寄駅線路名']},#{info['③最寄り駅名']},#{info['③最寄り駅まで所要時間']},#{info['④最寄駅線路名']},#{info['④最寄り駅名']},#{info['④最寄り駅まで所要時間']},#{info['階数']},#{info['賃料']},#{info['管理費']},#{info['敷礼']},#{info['保証']},#{info['敷引']},#{info['償却']},#{info['間取り']},#{info['専有面積']},#{info['構造']},#{info['築年月']},#{info['駐車場まで距離']},#{info['駐車場賃料']},#{info['間取り詳細']},#{info['階建']},#{info['条件']},#{info['物件取扱店舗']},#{info['物件取扱店舗住所']},#{info['物件取扱店舗営業時間']},#{info['物件取扱店舗定休日']},#{info['物件取扱店舗電話番号']}\n" open("結果.csv", 'a'){|io| io.write(res.tosjis) } end

end until false

open('9.html', 'w'){|io| io.write(page.body) }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問