Last active
November 18, 2017 05:21
-
-
Save baobao/02c6d638d9285bdff70852507011f841 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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