Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active November 18, 2017 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/02c6d638d9285bdff70852507011f841 to your computer and use it in GitHub Desktop.
Save baobao/02c6d638d9285bdff70852507011f841 to your computer and use it in GitHub Desktop.
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