前提・実現したいこと
現在、google colaboratoryを使用して、
下記のURLの市町村ごとの賃料を抽出し、CSVでエクスポートをしようとしております。
https://www.homes.co.jp/chintai/ibaraki/city/price/
「ワンルーム・1K・1DK」 の市町村ごとの賃料の抽出はできたのですが、
「1LDK・2K・2DK」、「2LDK・3K・3DK」、「3LDK・4K・4DK」のタブを切り替えた時の市町村ごとの賃料の抽出ができません。
タブの切り替えをしても同じURLなので、どのようにコードを書けばよいかわからず。
ご多忙の中、大変恐縮ですが、どなたかご教示いただけますと大変幸いです。
該当のソースコード
Python
pandas as pd # pandasのインポート from bs4 import BeautifulSoup # BeautifulSoupのインポート import requests # requestsのインポート from google.colab import files url = "https://www.homes.co.jp/chintai/ibaraki/city/price/" response = requests.get(url, timeout=3.5).text soup = BeautifulSoup(response, 'html.parser') # BeautifulSoupの初期化 tags = soup.find(id= "prg-aggregate-graph") locations = tags.select("tr") columns = ["place", "price"] df2 = pd.DataFrame(columns=columns) for location in locations: place = location.a.string price = location.span.string se = pd.Series([place, price], columns) print (se) df2 = df2.append(se, columns) filename = "result.csv" df2.to_csv(filename, encoding = 'utf-8-sig') files.download(filename)
あなたの回答
tips
プレビュー