Published Date : 2020年9月10日16:56

【python】郵便番号から住所と緯度経度を取得してMapにプロットする: Part 1
【python】Gets the address and latitude and longitude from the postal code and plots it on Map: Part 1


This blog has an English translation


YouTubeにアップした動画、「【python】郵便番号から住所と緯度経度を取得してMapにプロットする: Part 1」の補足説明の記事です。

Here's a little more about the 「【python】Gets the address and latitude and longitude from the postal code and plots it on Map: Part 1」 video I uploaded to YouTube.


目次

Table of Contents




① 動画の説明
① Video Description



郵便番号から住所と緯度経度を取得してMapにプロットする パート1

Gets the address and latitude and longitude from the zip code and plots it on Map Part 1

Part1の今回は日本郵便のサイトから各都道府県の郵便番号のcsvをダウンロードして、

This time, part 1, I will download the csv of the zip code of each prefecture from the Japan Post site,

郵便番号から住所を検索、それを緯度経度に変換することまでを行います。

search the address from the postal code, and convert it to latitude and longitude.

日本郵便のサイトにいき、希望の都道府県のCSVファイルをダウンロードして解凍してください。

Please access to the Japan Post site, then download the CSV file of your desired prefecture and unzip it.

ダウンロードしたCSVファイルを開いてください。CSVファイルの中身の構造を確認したら、Pythonのスクリプトを書いていきます。

Open the downloaded CSV file. Once you've seen the structure of the contents of the CSV file, write a Python script.

Pandasをインポートして、エンコードにshift-jisを指定して、CSVファイルを読み込みます。

Import the CSV file by importing Pandas and specifying shift-jis encoding.

ご覧の通り、一行目がカラムになってしまっているので、直しましょう。

As you can see, the first line is a column, so let's fix it.

やり方は簡単です、csvを読み込む際にキーワード引数のheaderをNoneにするだけです。

The easy way to do this is to set the keyword argument header to None when reading csv.

取得したいのは郵便番号なので、特にカラムを分かりやすくする必要はありませんが、もしカスタムカラムを作りたければ、このようにします。

You don't need to make the column more descriptive because you want to get the postal code, but if you want to create a custom column, you can do this.

なぜPandasを使っているのかというと、このCSVファイルを別の解析用途に使用したりとデータを応用できる範囲が広がるからです。

The reason I'm using Pandas is that it allows me to use this CSV file for other analysis purposes and to apply the data to a wider range of applications.

では郵便番号のみを抽出しましょう。そしてその郵便番号を使って、

Then, let's extract only zip codes. You can then use Selenium

郵便番号から住所と店を検索するサイトへSeleniumを使ってアクセスして、データを収集していきます。

to collect data from sites that use the zip code to search for addresses and stores.

まずは必要なモジュール等をインポートします。

First, import the necessary modules.

次に郵便番号を取り出しやすくするため、一つのリストにします。

Next, to make it easier to retrieve the postal codes, I'll put them on a list.

結果のデータを格納する空のリストを用意します。

Provides an empty list to store the resulting data.

何度も使用するので、必要なXPATHを変数にしておきます。

You will use it again and again, so make the required XPATH a variable.

そしてクロームドライバーを使いクロームブラウザを立ち上げて、ターゲットのサイトにSeleniumを使ってアクセスします。

Then use the Chrome driver to launch Chrome Browser and access the target site using by Selenium.

試しに100個ほど郵便番号を検索してみましょう。

Let's try searching about 100 postal codes.

数が4000行ほどありますので、少しの量で試すことをお勧めします。

There are about 4000 lines, so I recommend you try a small amount.

後、相手のサーバーに負担をかけないようにtimeモジュールを使ってインターバルを数秒設けることを忘れずに行ってください。

Also, don't forget to use the time module to set an interval of a few seconds so as not to strain the other server.

データフレームに直して表示させてみましょう。

Let's change it to a data frame and display it.

重複している行がいくつかありますが、pandasを使えば、一瞬で重複している行を削除してまとめることができます。

There are some duplicate lines, but you can use pandas to quickly group them together.



import time
import re
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import Chrome, ChromeOptions
import pandas as pd

csv_path = r"C:\Users\user\13TOKYO.CSV"

df = pd.read_csv(csv_path, header=None, encoding="SHIFT-JIS")

zip_code_list = df[2].to_list()

results = []

URL = "https://"
driver_path = r'C:\Users\user\chromedriver.exe'

input_xpath = '//input[@name=""]'
searchbtn_xpath = '//input[@name=""]'
searchresult_xpath = '//div[@id=""]'

options = ChromeOptions()
options.add_argument('--incognito')

driver = Chrome(
    executable_path=driver_path, options=options)

driver.get(URL)

try:
    WebDriverWait(driver, 6).until(
        EC.presence_of_element_located((By.XPATH, input_xpath)))
except:
    time.sleep(3)

for zip_code in zip_code_list:

    inputPostalCode = driver.find_element_by_xpath(input_xpath)
    inputPostalCode.clear()
    inputPostalCode.send_keys(zip_code)
    searchbtn = driver.find_element_by_xpath(searchbtn_xpath)
    searchbtn.click()

    try:
        WebDriverWait(driver, 6).until(
            EC.presence_of_element_located((By.XPATH, searchresult_xpath)))
    except:
        time.sleep(3)

    searchresult = driver.find_element_by_xpath(searchresult_xpath)

    if searchresult.text == '郵便番号が存在しません。':
        print(searchresult.text)
    else:
        searchresult = searchresult.find_elements_by_xpath(
            './section[@class=""]')
        for sr in searchresult:
            result_dict = {}
            result_dict['名前'] = sr.text.split('\n')[0]
            result_dict['住所'] = sr.text.split('\n')[1].replace('住所', '')
            result_dict['TEL'] = sr.text.split('\n')[2].replace('TEL', '')
            result_dict['FAX'] = sr.text.split('\n')[3].replace('FAX', '')
            results.append(result_dict)
    time.sleep(1)

driver.quit()
address_df = pd.DataFrame(results, columns=results[0].keys())
address_df = address_df.drop_duplicates()

address_df.to_csv('address.csv', index=None)

以上です。お疲れ様です。

That's all. Thank you for your hard work.