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