はじめまして。
pythonを勉強し始めてまだ間もない者です。
勉強のためにホームページに書かれているコードを参照して学習しています。
そこで、「機械学習を使って東京23区のお買い得賃貸物件を探してみた 〜スクレイピング編〜」にかかれているコードを拝借さしてもらいました。
url;http://www.analyze-world.com/entry/2017/10/09/062445
コード
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
python
1 2# coding: utf-8 3 4#必要なライブラリをインポート 5from bs4 import BeautifulSoup 6import requests 7import pandas as pd 8from pandas import Series, DataFrame 9import time 10 11#URL(東京都足立区の賃貸住宅情報 検索結果の1ページ目) 12url = 'http://suumo.jp/jj/chintai/ichiran/FR301FC001/?ar=030&bs=040&ta=13&sc=13121&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' 13 14#データ取得 15result = requests.get(url) 16c = result.content 17 18#HTMLを元に、オブジェクトを作る 19soup = BeautifulSoup(c,"html.parser") 20 21#物件リストの部分を切り出し 22summary = soup.find("div",{'id':'js-bukkenList'}) 23 24#ページ数を取得 25body = soup.find("body") 26pages = body.find_all("div",{'class':'pagination pagination_set-nav'}) 27pages_text = str(pages) 28pages_split = pages_text.split('</a></li>\n</ol>') 29pages_split0 = pages_split[0] 30pages_split1 = pages_split0[-3:] 31pages_split2 = pages_split1.replace('>','') 32pages_split3 = int(pages_split2) 33 34#URLを入れるリスト 35urls = [] 36 37#1ページ目を格納 38urls.append(url) 39 40#2ページ目から最後のページまでを格納 41for i in range(pages_split3-1): 42 pg = str(i+2) 43 url_page = url + '&pn=' + pg 44 urls.append(url_page) 45 46name = [] #マンション名 47address = [] #住所 48locations0 = [] #立地1つ目(最寄駅/徒歩~分) 49locations1 = [] #立地2つ目(最寄駅/徒歩~分) 50locations2 = [] #立地3つ目(最寄駅/徒歩~分) 51age = [] #築年数 52height = [] #建物高さ 53floor = [] #階 54rent = [] #賃料 55admin = [] #管理費 56others = [] #敷/礼/保証/敷引,償却 57floor_plan = [] #間取り 58area = [] #専有面積 59 60#各ページで以下の動作をループ 61for url in urls: 62 #物件リストを切り出し 63 result = requests.get(url) 64 c = result.content 65 soup = BeautifulSoup(c) 66 summary = soup.find("div",{'id':'js-bukkenList'}) 67 68 #マンション名、住所、立地(最寄駅/徒歩~分)、築年数、建物高さが入っているcassetteitemを全て抜き出し 69 cassetteitems = summary.find_all("div",{'class':'cassetteitem'}) 70 71 #各cassetteitemsに対し、以下の動作をループ 72 for i in range(len(cassetteitems)): 73 #各建物から売りに出ている部屋数を取得 74 tbodies = cassetteitems[i].find_all('tbody') 75 76 #マンション名取得 77 subtitle = cassetteitems[i].find_all("div",{ 78 'class':'cassetteitem_content-title'}) 79 subtitle = str(subtitle) 80 subtitle_rep = subtitle.replace( 81 '[<div class="cassetteitem_content-title">', '') 82 subtitle_rep2 = subtitle_rep.replace( 83 '</div>]', '') 84 85 #住所取得 86 subaddress = cassetteitems[i].find_all("li",{ 87 'class':'cassetteitem_detail-col1'}) 88 subaddress = str(subaddress) 89 subaddress_rep = subaddress.replace( 90 '[<li class="cassetteitem_detail-col1">', '') 91 subaddress_rep2 = subaddress_rep.replace( 92 '</li>]', '') 93 94 #部屋数だけ、マンション名と住所を繰り返しリストに格納(部屋情報と数を合致させるため) 95 for y in range(len(tbodies)): 96 name.append(subtitle_rep2) 97 address.append(subaddress_rep2) 98 99 #立地を取得 100 sublocations = cassetteitems[i].find_all("li",{ 101 'class':'cassetteitem_detail-col2'}) 102 103 #立地は、1つ目から3つ目までを取得(4つ目以降は無視) 104 for x in sublocations: 105 cols = x.find_all('div') 106 for i in range(len(cols)): 107 text = cols[i].find(text=True) 108 for y in range(len(tbodies)): 109 if i == 0: 110 locations0.append(text) 111 elif i == 1: 112 locations1.append(text) 113 elif i == 2: 114 locations2.append(text) 115 116 #築年数と建物高さを取得 117 tbodies = cassetteitems[i].find_all('tbody') 118 col3 = cassetteitems[i].find_all("li",{ 119 'class':'cassetteitem_detail-col3'}) 120 for x in col3: 121 cols = x.find_all('div') 122 for i in range(len(cols)): 123 text = cols[i].find(text=True) 124 for y in range(len(tbodies)): 125 if i == 0: 126 age.append(text) 127 else: 128 height.append(text) 129 130 #階、賃料、管理費、敷/礼/保証/敷引,償却、間取り、専有面積が入っているtableを全て抜き出し 131 tables = summary.find_all('table') 132 133 #各建物(table)に対して、売りに出ている部屋(row)を取得 134 rows = [] 135 for i in range(len(tables)): 136 rows.append(tables[i].find_all('tr')) 137 138 #各部屋に対して、tableに入っているtext情報を取得し、dataリストに格納 139 data = [] 140 for row in rows: 141 for tr in row: 142 cols = tr.find_all('td') 143 for td in cols: 144 text = td.find(text=True) 145 data.append(text) 146 147 #dataリストから、階、賃料、管理費、敷/礼/保証/敷引,償却、間取り、専有面積を順番に取り出す 148 index = 0 149 for item in data: 150 if '階' in item: 151 floor.append(data[index]) 152 rent.append(data[index+1]) 153 admin.append(data[index+2]) 154 others.append(data[index+3]) 155 floor_plan.append(data[index+4]) 156 area.append(data[index+5]) 157 index +=1 158 159 #プログラムを10秒間停止する(スクレイピングマナー) 160 time.sleep(10) 161 162#各リストをシリーズ化 163name = Series(name) 164address = Series(address) 165locations0 = Series(locations0) 166locations1 = Series(locations1) 167locations2 = Series(locations2) 168age = Series(age) 169height = Series(height) 170floor = Series(floor) 171rent = Series(rent) 172admin = Series(admin) 173others = Series(others) 174floor_plan = Series(floor_plan) 175area = Series(area) 176 177#各シリーズをデータフレーム化 178suumo_df = pd.concat([name, address, locations0, locations1, locations2, age, height, floor, rent, admin, others, floor_plan, area], axis=1) 179 180#カラム名 181suumo_df.columns=['マンション名','住所','立地1','立地2','立地3','築年数','建物高さ','階','賃料','管理費', '敷/礼/保証/敷引,償却','間取り','専有面積'] 182 183#csvファイルとして保存 184suumo_df.to_csv('suumo_adachi.csv', sep = '\t',encoding='utf-16') 185```python 186 187で実行すると 188 189 pages_split3 = int(pages_split2) 190ValueError: invalid literal for int() with base 10: 'v]' 191 192というエラーが怒ってしまいます 193intに文字列が入れれないところまでは 194googleで調べてわかったのですが、 195その後の対応策がわかりません。 196どなたかわかる方いましたら教えてください!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/23 04:19
2017/11/23 04:26
2017/11/23 04:34
2017/11/23 04:35
2017/11/23 04:43 編集
2017/11/23 04:52
2017/11/23 04:56
2017/11/23 05:01
2017/11/23 05:10 編集
2017/11/23 05:13
2017/11/23 05:18
2017/11/23 05:28