Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active Nov 18, 2017
Embed
What would you like to do?
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment