from bs4 import BeautifulSoup
import urllib.request as req
import os.path
# xmlをダウンロード
url = "http://www.city.yokohama.lg.jp/somu/org/kikikanri/data/shelter.xml"
savename = "shelter.xml"
if not os.path.exists(savename):
req.urlretrieve(url, savename)
# ダウンロードしたxmlを開いてパース
xml = open(savename, "r", encoding="utf-8").read()
soup = BeautifulSoup(xml, 'html.parser')
info = {}
for obj in soup.find_all("shelter"):
name = obj.find('name').string
ward = obj.find('ward').string
address = obj.find('address').string
note = obj.find('notes').string
if not (ward in info):
info[ward] = []
info[ward].append(name)
# 結果出力
for ward in info.keys():
print("+", ward)
for name in info[ward]:
print("| -", name)