横浜市の防災拠点
from bs4 import BeautifulSoup
import urllib.request as req
import os.path
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 = open(savename, "r", encoding="utf-8").read()
soup = BeautifulSoup(xml, 'html.parser')
info = {}
for i in soup.find_all("shelter"):
name = i.find("name").string
ward = i.find("ward").string
addr = i.find("address").string
note = i.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)
[vagrant@localhost python]$ python3 app.py
+ 磯子区
| – 滝頭小学校
| – 岡村小学校
| – 磯子小学校
| – 浜小学校
| – 杉田小学校
| – 根岸小学校
| – 屏風浦小学校
| – 梅林小学校
| – 汐見台小学校
| – 洋光台第一小学校
| – 洋光台第二小学校
| – さわの里小学校
| – 洋光台第三小学校
| – 洋光台第四小学校
| – 森東小学校
| – 山王台小学校
…
なるほど。xmlを作って公開したいですな。
<?php
$rootNode = new SimpleXMLElement("<rss xmlns:a10='http://www.w3.org/2005/Atom' version='2.0'><items></items>");
$itemNode = $rootNode->addChild('item');
$itemNode->addChild('title', '剰余金の配当に関するお知らせ');
$itemNode->addChild('description', '剰余金の配当を行うことを決議いたしました。');
$itemNode = $rootNode->addChild('item');
$itemNode->addChild('title', '監査役の異動に関するお知らせ');
$itemNode->addChild('description', '定時株主総会に付議する監査役の候補者につきまして、下記の通り内定いたしました。');
$itemNode = $rootNode->addChild('item');
$itemNode->addChild('title', '自己株式の取得状況に関するお知らせ');
$itemNode->addChild('description', '自己株式取得状況について、下記のとおりお知らせいたします。');
// $itemNode->addAttribute('stock', 'none');
$dom = new DOMDocument('1.0');
$dom->loadXML($rootNode->asXML());
$dom->formatOutput = true;
echo $dom->saveXML();
[Sat Aug 11 20:12:20 2018] 192.168.35.1:53532 [500]: /app.php – Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /home/vagrant/python/app.php:3
Stack trace:
#0 /home/vagrant/python/app.php(3): SimpleXMLElement->__construct(‘
[/php]