[PYTHON] Get the address from the zip code

Used when you want to specify the address from the zip code

Use the post office area search https://www.post.japanpost.jp/zipcode/index.html

A function that parses the result of the above form and returns the return value as a dictionary

import requests
from bs4 import BeautifulSoup
import re


pref_key = {
    'Hokkaido': 1,
    'Aomori Prefecture': 2,
    'Iwate Prefecture': 3,
    'Miyagi Prefecture': 4,
    'Akita': 5,
    'Yamagata Prefecture': 6,
    'Fukushima Prefecture': 7,
    'Ibaraki Prefecture': 8,
    'Tochigi Prefecture': 9,
    'Gunma Prefecture': 10,
    'Saitama': 11,
    'Chiba': 12,
    'Tokyo': 13,
    'Kanagawa Prefecture': 14,
    'Niigata Prefecture': 15,
    'Toyama Prefecture': 16,
    'Ishikawa Prefecture': 17,
    'Fukui prefecture': 18,
    'Yamanashi Prefecture': 19,
    'Nagano Prefecture': 20,
    'Gifu Prefecture': 21,
    'Shizuoka Prefecture': 22,
    'Aichi prefecture': 23,
    'Mie Prefecture': 24,
    'Shiga Prefecture': 25,
    'Kyoto': 26,
    'Osaka': 27,
    'Hyogo prefecture': 28,
    'Nara Prefecture': 29,
    'Wakayama Prefecture': 30,
    'Tottori prefecture': 31,
    'Shimane Prefecture': 32,
    'Okayama Prefecture': 33,
    'Hiroshima Prefecture': 34,
    'Yamaguchi Prefecture': 35,
    'Tokushima Prefecture': 36,
    'Kagawa Prefecture': 37,
    'Ehime Prefecture': 38,
    'Kochi Prefecture': 39,
    'Fukuoka Prefecture': 40,
    'Saga Prefecture': 41,
    'Nagasaki Prefecture': 42,
    'Kumamoto Prefecture': 43,
    'Oita Prefecture': 44,
    'Miyazaki prefecture': 45,
    'Kagoshima prefecture': 46,
    'Okinawa Prefecture': 47
}


def get_html_text(url: str) -> str:
    try:
        req = requests.get(url)
    except requests.exceptions.ConnectionError:
        return False
    return req.content


def getAddress(postal_code: int) -> dict:
    url = f'https://www.post.japanpost.jp/smt-zipcode/zipcode.php?zip={postal_code}'
    content = get_html_text(url)
    if not content:
        return False
    soup = BeautifulSoup(content, 'html.parser')
    dds = [i.text.strip() for i in soup.body.findAll('dd')]
    if not dds:
        return False
    dds = dds[:4]
    info = {
        'postal': dds[0].replace('-', ''),
        'pref': dds[1],
        'city': dds[2],
        'address': re.sub(r'\(.*', '', dds[3])
    }
    info['pref_no'] = pref_key[info['pref']]
    return info


if __name__ == "__main__":
    data = getAddress(1000004)
    print(data)

"""
result
{
  'postal': '1000004', 
  'pref': 'Tokyo', 
  'city': 'Chiyoda Ward', 
  'address': 'Otemachi (excluding the following buildings, JA Building)', 
  'pref_no': 13
}
""" 

Recommended Posts

Get the address from the zip code
Get the address from latitude and longitude
Get the return code of the Python script from bat
[Django] Create a form that automatically fills in the address from the zip code
Get the value from the [Django] Form
Get the IPv4 address assigned to the network interface in code (Linux)
[Python] Get the character code of the file
Get the EDINET code list in Python
Get wordpress posts from the past week
[Python] Get the main color from the screenshot
Get only the text from the Django form.
Zip code geocoding
Get the contents of git diff from python
Get the GNOME version
[Python] Get the text of the law from the e-GOV Law API
Get local IP address
Get the MIME Type
Interactive console application for address and zip code search
Call the code generated by Cython from C / C ++ (Windows version)
Let Code Day 64 starting from zero "287. Find the Duplicate Number"
Get your heart rate from the fitbit API in Python!
Get the package version to register with PyPI from Git
Call the code generated by Cython from C / C ++ (Mac version)
Get the value while specifying the default value from dict in Python
Let Code Day2 Starting from Zero "1108. Defanging an IP Address"
System trade starting with Python3: Get the latest program code
I tried to get various information from the codeforces API
Get structural data from CHEMBLID
Get country code with python
The Kernel Address Sanitizer (KASAN) (2/2)
Let's search from the procession
Install ansible from source code
Explain the code of Tensorflow_in_ROS
Remove the frame from the image
I tweeted from the terminal!
[Python] Get the previous month
Stop Omxplayer from Python code
The Kernel Address Sanitizer (KASAN) (1/2)
Maya | Get the workspace path
Check the increase / decrease of Bitcoin for each address from the blockchain
How to get followers and followers from python using the Mastodon API
[Python] Get the update date of a news article from HTML
I'll get rid of every single imperial system from the world!