4
2

【スクレイピング】SUUMOのサイトから物件情報を取得する~Tkinterの実装~

Last updated at Posted at 2022-05-12

はじめに

※作成目的は個人使用であり、suumoの利用規約によって商用利用が禁止されているので絶対に悪用しないでください

スクレイピングに加えてTkinterも実装しましたが、コード全体の記述量が多いため二部構成にしたいと思います。本記事はTkinterの実装についてのまとめですので、スクレイピング部分は以下の記事をご参照ください。

環境

・OS: Windows10
・開発言語: Python3.10
・エディタ: VSCode
・ブラウザ: chrome

使用するライブラリ

pip install selenium

 スクレイピングを行うためのライブラリはいくつかありますが、今回はSeleniumを利用します。併せてchromedriverも使用しますが、こちらのインストール方法につきましては他の方の記事を参考にしていただけると幸いです。
 また、今回は取得した情報をDiscordに出力する形をとったので

pip install discordwebhook

も併せて実行します。

コード

コード説明の前に、まずはこれからTkinterで作成するUIがどのようなものかをお見せしたいと思います。
image.png
Pysideを用いることで、おしゃれなUIを作成することも可能だそうです。今回はスクレイピングを主目的としていますので、UI部分はシンプルかつ習得が容易だったTkinterを採用しました。
※GUIとUIの違いを正しく理解していませんでした…今回作成したのはUIですので、表記を訂正しました。(2024年2月20日)

ではコードを見ていきましょう
はじめに必要なライブラリをインポートします

from selenium import webdriver
import tkinter
import time
import re
from discordwebhook import Discord

driver = webdriver.Chrome() #←パスが通っていない場合はchromedriver.exeを置いているディレクトリを指定

ここからUIを作っていきます

###UI作成###
app = tkinter.Tk()
app.title(u'SUUMO 賃貸物件検索')
app.geometry("920x670")
label_font = '游明朝'

画像上部の「検索するエリアの設定」の部分です
検索したいエリアや路線を指定し、物件の一覧を表示した状態のURLをここに貼り付けます。先に「間取りタイプ」や「建物の種類」等の絞り込み条件にチェックを入れてURLを渡すと、この後の条件指定が正しく処理できないので注意してください。
また、完成図のような配置にするために複数のフレームを使用します。コードが非常にややこしくなっているので簡単な図解を添えておきます。
スクリーンショット 2022-05-12 083820.png

スクリーンショット 2022-05-12 085100.png

##URL
url_frame = tkinter.LabelFrame(app,text='検索するエリアの設定',background='white',font=label_font,bg='navajowhite')
url_frame.grid(row=1,column=1,padx=40,pady=0,ipadx=0,ipady=0,sticky=tkinter.N)
url_static = tkinter.Label(url_frame, text=u"'ctrl+v'でURLを貼り付けてください",bg='white',font=label_font)
url_static.grid(row=2,column=1,padx=18,pady=0,ipadx=0,ipady=2,sticky=tkinter.W)
url_entry = tkinter.Entry(url_frame, width=20,bg='gray97')
url_entry.grid(row=3, column=1, padx=20,pady=2,ipadx=87,ipady=0)

#ページ遷移回数
countpage_frame = tkinter.LabelFrame(app,text='取得ページ数',background='white',font=label_font,bg='navajowhite')
countpage_frame.grid(row=1,column=2,padx=10,pady=0,ipadx=0,ipady=0,sticky=tkinter.W)
page_static = tkinter.Label(url_frame, text=u"取得ページ数を指定",bg='white',font=label_font)
page_static.grid(row=2,column=2,padx=8,pady=0,ipadx=0,ipady=2,sticky=tkinter.W)
page_entry = tkinter.Entry(url_frame, width=25,bg='gray97')
page_entry.grid(row=3, column=2, padx=5,pady=2,ipadx=10,ipady=0)

間取り条件のラベルと入力用ボックス作り

「間取り」の項目では複数選択が可能なチェックボックス(BooleanVar)を使用します。
"boolean_数字.set(False)"はチェックボックスの初期値(最初からチェックを入れるか)を指定し、チェックがついていない状態にしています。
なお、

bool_width = 8
check_row = 3
check_column = 1

で事前に列と行を指定するための値を宣言しています。これにより、異なる値を使いたい場合は"変数名+1"のようにコードを書いている箇所で自由に値を変化させることができます。また、「やっぱり全体に1ずつ足したい!」といった変更にも予め宣言した値を変更するだけでばっちり対応できます。

チェックボックスを作る際、その項目名、つまりラベルも添えて区別がつくようにしなければなりませんが、チェックボックスとラベルは異なる列に配置する必要があります。今回はチェックボックス→ラベルの順番で書いているので、ラベルの列は"列の値を与えた変数+1"で一致させたいボックスのひとつ隣を指定しています。

##間取り
bool_label = tkinter.Label(condition_frame,text='間取り',bg='azure',relief='groove',font=label_font)
bool_label.grid(row=1,column=1,padx=0,pady=0,ipadx=10,ipady=7,sticky=tkinter.N)
boolean = tkinter.BooleanVar()
boolean.set(False) #初期状態ではオフに
sort_list = ['ワンルーム','1K','1DK','1LDK','2K','2DK','2LDK','3K','3DK','3LDK','4K','4DK','4LDK','5K以上']
bool_width = 8
check_row = 3
check_column = 1

#タイトル「間取り」と分離
inner_boolframe = tkinter.LabelFrame(condition_frame)
inner_boolframe.grid(row=2,column=1,padx=20,pady=0,ipadx=0,ipady=0,sticky=tkinter.N)
#ワンルーム
boolean_0 = tkinter.BooleanVar()
boolean_0.set(False) #初期状態ではオフに
check_button0 = tkinter.Checkbutton(inner_boolframe, variable = boolean_0, width =1,text = '',)
check_button0.grid(row = check_row, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[0],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#1K
boolean_1 = tkinter.BooleanVar()
boolean_1.set(False) #初期状態ではオフに
check_button1 = tkinter.Checkbutton(inner_boolframe, variable = boolean_1, width =1,text = '')
check_button1.grid(row=check_row + 1, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[1],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 1 , column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#1DK
boolean_2 = tkinter.BooleanVar()
boolean_2.set(False) #初期状態ではオフに
check_button2 = tkinter.Checkbutton(inner_boolframe, variable = boolean_2, width =1,text = '')
check_button2.grid(row=check_row + 2, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[2],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 2, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#1LDK
boolean_3 = tkinter.BooleanVar()
boolean_3.set(False) #初期状態ではオフに
check_button3 = tkinter.Checkbutton(inner_boolframe, variable = boolean_3, width =1,text = '')
check_button3.grid(row=check_row + 3, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[3],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 3, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#2K
boolean_4 = tkinter.BooleanVar()
boolean_4.set(False) #初期状態ではオフに
check_button4 = tkinter.Checkbutton(inner_boolframe, variable = boolean_4, width =1,text = '')
check_button4.grid(row=check_row + 4, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[4],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 4, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#2DK
boolean_5 = tkinter.BooleanVar()
boolean_5.set(False) #初期状態ではオフに
check_button5 = tkinter.Checkbutton(inner_boolframe, variable = boolean_5, width =1,text = '')
check_button5.grid(row=check_row + 5, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[5],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 5, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#2LDK
boolean_6 = tkinter.BooleanVar()
boolean_6.set(False) #初期状態ではオフに
check_button6 = tkinter.Checkbutton(inner_boolframe, variable = boolean_6, width =1,text = '')
check_button6.grid(row=check_row + 6, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[6],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 6, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#3K
boolean_7 = tkinter.BooleanVar()
boolean_7.set(False) #初期状態ではオフに
check_button7 = tkinter.Checkbutton(inner_boolframe, variable = boolean_7, width =1,text = '')
check_button7.grid(row=check_row + 7, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[7],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 7, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#3DK
boolean_8 = tkinter.BooleanVar()
boolean_8.set(False) #初期状態ではオフに
check_button8 = tkinter.Checkbutton(inner_boolframe, variable = boolean_8, width =1,text = '')
check_button8.grid(row=check_row + 8, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[8],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 8, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#3LDK
boolean_9 = tkinter.BooleanVar()
boolean_9.set(False) #初期状態ではオフに
check_button9 = tkinter.Checkbutton(inner_boolframe, variable = boolean_9, width =1,text = '')
check_button9.grid(row=check_row + 9, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[9],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 9, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#4K
boolean_10 = tkinter.BooleanVar()
boolean_10.set(False) #初期状態ではオフに
check_button10 = tkinter.Checkbutton(inner_boolframe, variable = boolean_10, width =1,text = '')
check_button10.grid(row=check_row + 10, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[10],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 10, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#4DK
boolean_11 = tkinter.BooleanVar()
boolean_11.set(False) #初期状態ではオフに
check_button11 = tkinter.Checkbutton(inner_boolframe, variable = boolean_11, width =1,text = '')
check_button11.grid(row=check_row + 11, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[11],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 11, column=check_column+1 ,padx=0,pady=0,ipadx=0,ipady=0)
#4LDK
boolean_12 = tkinter.BooleanVar()
boolean_12.set(False) #初期状態ではオフに
check_button12 = tkinter.Checkbutton(inner_boolframe, variable = boolean_12, width =1,text = '')
check_button12.grid(row=check_row + 12, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[12],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 12, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#5K~
boolean_13 = tkinter.BooleanVar()
boolean_13.set(False) #初期状態ではオフに
check_button13 = tkinter.Checkbutton(inner_boolframe, variable = boolean_13, width =1,text = '')
check_button13.grid(row=check_row + 13, column=check_column, padx=0,pady=0,ipadx=0,ipady=0)
sort_word=tkinter.Label(inner_boolframe,width=bool_width,text=sort_list[13],background='white',anchor='w',font=label_font)
sort_word.grid(row=check_row + 13, column=check_column+1, padx=0,pady=0,ipadx=0,ipady=0)

数値で指定する条件のボックス作り

文字列(家賃の金額など)を入力するためにEntry()を用います。
ここでは、上から順に
ラベル→ボックス→ラベル→ボックス→…という構造ですので、列は全て同じ値を指定して大丈夫です!ただし、行は1ずつ足してお互いに被らないようにしましょう。

##入力
entry_frame = tkinter.Label(condition_frame,text='賃料等の設定',background='white',bg='lightskyblue',relief='groove',font=label_font)
entry_frame.grid(row=1,column=2,padx=10,pady=0,ipadx=10,ipady=7,sticky=tkinter.N)

enetry_width = 20
entry_row = 3
entry_column = 6

inner_enetryframe = tkinter.LabelFrame(condition_frame,bg='white')
inner_enetryframe.grid(row=2,column=2,padx=10,pady=0,ipadx=0,ipady=5,sticky=tkinter.N)
#築年数
Static1 = tkinter.Label(inner_enetryframe, text=u'築年数の下限',bg='white',font=label_font)
Static1.grid(row=entry_row, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
min_year_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
min_year_box.grid(row=entry_row + 2, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#築年数
Static2 = tkinter.Label(inner_enetryframe, text=u'築年数の上限',bg='white',font=label_font)
Static2.grid(row=entry_row + 3, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
max_year_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
max_year_box.grid(row=entry_row + 4, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#家賃+管理費
Static3 = tkinter.Label(inner_enetryframe, text=u'家賃+管理費の下限',bg='white',font=label_font)
Static3.grid(row=entry_row + 5, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
min_price_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
min_price_box.grid(row=entry_row + 6, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#家賃+管理費
Static4 = tkinter.Label(inner_enetryframe, text=u'家賃+管理費の上限',bg='white',font=label_font)
Static4.grid(row=entry_row + 7, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
max_price_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
max_price_box.grid(row=entry_row + 8, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#敷金
Static5 = tkinter.Label(inner_enetryframe, text=u'敷金の下限',bg='white',font=label_font)
Static5.grid(row=entry_row + 9, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
min_deposit_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
min_deposit_box.grid(row=entry_row + 10, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#敷金
Static6 = tkinter.Label(inner_enetryframe, text=u'敷金の上限',bg='white',font=label_font)
Static6.grid(row=entry_row + 11, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
max_deposit_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
max_deposit_box.grid(row=entry_row + 12, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#礼金
Static7 = tkinter.Label(inner_enetryframe, text=u'礼金の下限',bg='white',font=label_font)
Static7.grid(row=entry_row + 13, column=entry_column, padx=0,pady=0,ipadx=0,ipady=0)
min_key_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
min_key_box.grid(row=entry_row + 14, column=entry_column, padx=0,pady=0,ipadx=0,ipady=1)

#礼金
Static8 = tkinter.Label(inner_enetryframe, text=u'礼金の上限',bg='white',font=label_font)
Static8.grid(row=entry_row + 15, column=entry_column, padx=0,pady=2,ipadx=0,ipady=0)
max_key_box = tkinter.Entry(inner_enetryframe, width=enetry_width,bg='gray97')
max_key_box.grid(row=entry_row + 16, column=entry_column, padx=0,pady=2,ipadx=0,ipady=1)

その他の条件

スーモで物件を探す際の便利な機能のひとつに、条件を追加で選択することができる「こだわり条件」があります。これらは1つでも、全部選択することもできるため、チェックボタンを使うのが良いでしょう。基本的な作り方は間取りのときと同様です。

##希望条件
idealhome_label = tkinter.Label(condition_frame,text='希望条件',background='white',bg='azure',relief='groove',font=label_font)
idealhome_label.grid(row=1,column=3,padx=10,pady=0,ipadx=10,ipady=7,sticky=tkinter.N)
deposit_key_list = ['敷金・保証金なし','礼金なし','駐車場あり','バス・トイレ別','ペット相談','2階以上住戸','室内洗濯機置き場','エアコン付き','オートロック','フローリング','間取り図付き','物件動画付き','パノラマ付き','定期借家を含まない']
deposit_key_width = 14
deposit_key_row = 3
deposit_key_column = 5

inner_idealframe = tkinter.LabelFrame(condition_frame)
inner_idealframe.grid(row=2,column=3,padx=20,pady=0,ipadx=0,ipady=0,sticky=tkinter.N)
#敷金・保証金なし
bool_non_deposit = tkinter.BooleanVar()
bool_non_deposit.set(False)
check_non_deposit = tkinter.Checkbutton(inner_idealframe,variable=bool_non_deposit,width=1,text='',background='white')
check_non_deposit.grid(row=deposit_key_row, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
label_deposit = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[0],background='white',anchor='w',font=label_font)
label_deposit.grid(row=deposit_key_row, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#礼金なし
bool_non_keymoney = tkinter.BooleanVar()
bool_non_keymoney.set(False)
check_non_keymoney = tkinter.Checkbutton(inner_idealframe,variable=bool_non_keymoney,width=1,text='',background='white')
check_non_keymoney.grid(row=deposit_key_row+1, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
label_keymoney = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[1],background='white',anchor='w',font=label_font)
label_keymoney.grid(row=deposit_key_row+1, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#駐車場あり
parking_bool = tkinter.BooleanVar()
parking_bool.set(False)
parking_check = tkinter.Checkbutton(inner_idealframe,variable=parking_bool, width=1,text='',background='white')
parking_check.grid(row=deposit_key_row + 2, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
parking_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[2], background='white',anchor='w',font=label_font)
parking_label.grid(row=deposit_key_row + 2, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#バス・トイレ別
separate_bool = tkinter.BooleanVar()
separate_bool.set(False)
separate_check = tkinter.Checkbutton(inner_idealframe,variable=separate_bool, width=1,text='',background='white')
separate_check.grid(row=deposit_key_row + 3, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
separate_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[3], background='white',anchor='w',font=label_font)
separate_label.grid(row=deposit_key_row + 3, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)
#ペット相談
pet_bool = tkinter.BooleanVar()
pet_bool.set(False)
pet_check = tkinter.Checkbutton(inner_idealframe,variable=pet_bool, width=1,text='',background='white')
pet_check.grid(row=deposit_key_row + 4, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
pet_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[4], background='white',anchor='w',font=label_font)
pet_label.grid(row=deposit_key_row + 4, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#2階以上住戸
stairs2_bool = tkinter.BooleanVar()
stairs2_bool.set(False)
stairs2_check = tkinter.Checkbutton(inner_idealframe,variable=stairs2_bool, width=1,text='',background='white')
stairs2_check.grid(row=deposit_key_row + 5, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
stairs2_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[5], background='white',anchor='w',font=label_font)
stairs2_label.grid(row=deposit_key_row + 5, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#室内洗濯機置き場
wash_bool = tkinter.BooleanVar()
wash_bool.set(False)
wash_check = tkinter.Checkbutton(inner_idealframe,variable=wash_bool, width=1,text='',background='white')
wash_check.grid(row=deposit_key_row + 6, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
wash_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[6], background='white',anchor='w',font=label_font)
wash_label.grid(row=deposit_key_row + 6, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#エアコン付き
air_bool = tkinter.BooleanVar()
air_bool.set(False)
air_check = tkinter.Checkbutton(inner_idealframe,variable=air_bool, width=1,text='',background='white')
air_check.grid(row=deposit_key_row + 7, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
air_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[7], background='white',anchor='w',font=label_font)
air_label.grid(row=deposit_key_row + 7, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#オートロック
autolock_bool = tkinter.BooleanVar()
autolock_bool.set(False)
autolock_check = tkinter.Checkbutton(inner_idealframe,variable=autolock_bool, width=1,text='',background='white')
autolock_check.grid(row=deposit_key_row + 8, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
autolock_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[8], background='white',anchor='w',font=label_font)
autolock_label.grid(row=deposit_key_row + 8, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#フローリング
flooring_bool = tkinter.BooleanVar()
flooring_bool.set(False)
flooring_check = tkinter.Checkbutton(inner_idealframe,variable=flooring_bool, width=1,text='',background='white')
flooring_check.grid(row=deposit_key_row + 9, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
flooring_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[9], background='white',anchor='w',font=label_font)
flooring_label.grid(row=deposit_key_row + 9, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#間取り図付き
planpic_bool = tkinter.BooleanVar()
planpic_bool.set(False)
planpic_check = tkinter.Checkbutton(inner_idealframe,variable=planpic_bool, width=1,text='',background='white')
planpic_check.grid(row=deposit_key_row + 10, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
planpic_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[10], background='white',anchor='w',font=label_font)
planpic_label.grid(row=deposit_key_row + 10, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

#物件動画付き
detailv_bool = tkinter.BooleanVar()
detailv_bool.set(False)
detailv_check = tkinter.Checkbutton(inner_idealframe,variable=detailv_bool, width=1,text='',background='white')
detailv_check.grid(row=deposit_key_row + 11, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
detailv_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[11], background='white',anchor='w',font=label_font)
detailv_label.grid(row=deposit_key_row + 11, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

# パノラマ付き
pano_bool = tkinter.BooleanVar()
pano_bool.set(False)
pano_check = tkinter.Checkbutton(inner_idealframe,variable=pano_bool, width=1,text='',background='white')
pano_check.grid(row=deposit_key_row + 12, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
pano_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[12], background='white',anchor='w',font=label_font)
pano_label.grid(row=deposit_key_row + 12, column=deposit_key_column+1, padx=0,pady=0,ipadx=0,ipady=0)

# 定期借家を含まない
nonregular_bool = tkinter.BooleanVar()
nonregular_bool.set(False)
nonregular_check = tkinter.Checkbutton(inner_idealframe,variable=nonregular_bool, width=1,text='',background='white')
nonregular_check.grid(row=deposit_key_row + 13, column=deposit_key_column, padx=0,pady=0,ipadx=0,ipady=0)
nonregular_label = tkinter.Label(inner_idealframe,width=deposit_key_width,text=deposit_key_list[13], background='white',anchor='w',font=label_font)
nonregular_label.grid(row=deposit_key_row + 13, column=deposit_key_column+1, padx=0,pady=0,ipadx=2,ipady=0)

ラジオボタンで物件の表示順を選択可能に

こだわり条件と同様に、スーモには物件を並べ替える便利な機能があります。おすすめ順、家賃+管理費が安い順などいくつかの選択肢から選ぶことができますが、ポイントは「1つしか選択できない」ことです。よって、チェックボックスではなくラジオボタンを使いましょう。

##並べ替え
radio_frame = tkinter.Label(condition_frame,text='並べ替え',background='white',bg='lightskyblue',relief='groove',font=label_font)
radio_frame.grid(row=1,column=4,padx=10,pady=0,ipadx=10,ipady=7,sticky=tkinter.N)
radio_value = tkinter.IntVar()
sort_list = ['おすすめ順','家賃+管理費が安い順','家賃+管理費が高い順','新着順','築年数が新しい順','専有面積が広い順','住所別']
radio_row = 2
radio_column = 4

inner_radioframe = tkinter.LabelFrame(condition_frame,background='white')
inner_radioframe.grid(row=2,column=4,padx=15,pady=0,ipadx=0,ipady=0,sticky=tkinter.N)
#おすすめ順
radio_recomend = tkinter.Radiobutton(inner_radioframe,text='おすすめ順',variable=radio_value,value=0,background='white',justify='left',font=label_font)
radio_recomend.grid(row=radio_row, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)
#家賃+管理費が安い順
radio_lowest = tkinter.Radiobutton(inner_radioframe,text='家賃+管理費が安い順',variable=radio_value,value=1,background='white',justify='left',font=label_font)
radio_lowest.grid(row=radio_row + 1, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)
#家賃+管理費が高い順
rario_highest = tkinter.Radiobutton(inner_radioframe,text='家賃+管理費が高い順',variable=radio_value,value=2,background='white',justify='left',font=label_font)
rario_highest.grid(row=radio_row + 2, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)
#新着順
radio_newest = tkinter.Radiobutton(inner_radioframe,text='新着順',variable=radio_value,value=3,background='white',justify='left',font=label_font)
radio_newest.grid(row=radio_row + 3, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)
#築年数が新しい順
radio_year = tkinter.Radiobutton(inner_radioframe,text='築年数が新しい順',variable=radio_value,value=4,background='white',justify='left',font=label_font)
radio_year.grid(row=radio_row + 4, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)
#専有面積が広い順
radio_occupied = tkinter.Radiobutton(inner_radioframe,text='専有面積が広い順',variable=radio_value,value=5,background='white',justify='left',font=label_font)
radio_occupied.grid(row=radio_row + 5, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)
#住所別
radio_adress = tkinter.Radiobutton(inner_radioframe,text='住所別',variable=radio_value,value=6,background='white',justify='left',font=label_font)
radio_adress.grid(row=radio_row + 6, column=radio_column, padx=0,pady=2,ipadx=0,ipady=0,sticky=tkinter.W)

最後に「OK」ボタンを作ります。
実際にプログラムを動かすとき、OKボタンを押せば入力した値がスクレイピング実行を命令する関数(次回の記事でご紹介します!)内で処理されるようになります。

###入力完了確認ボタン###
enter_button = tkinter.Button(text=u'OK',font=label_font)
enter_button.grid(row=3,column=1,padx=0,pady=0,ipadx=0,ipady=0)
enter_button.bind('<Button-1>',reload_suumo_search)
time.sleep(3)

app.mainloop()

おわりに

間取り~数値入力において、すべてのボタンをひとつずつ実装していますが、for文を使ってよりわかりやすく簡潔な実装が可能なようです。今の私の力量では実装できませんでしたが、改めて挑戦したいと思います。ご覧いただきありがとうございました!

4
2
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
4
2