目的
Windows 10 + Excel2016_x86 VBA で XML の基本的なことを少し触ってみる
で使用したXMLファイルをPythonで基本的な操作をもう少し試してみる。
Pythonのサンプルコード
基本的な機能をもう少し試してみる。
# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode
# coding:utf-8
from lxml import etree
import urllib.request
# urlencode
urladr = urllib.parse.urlencode({'addr':'池尻4-35-25'})
url = "http://geocode.csis.u-tokyo.ac.jp/cgi-bin/simple_geocode.cgi?charset=UTF8&" + urladr
with urllib.request.urlopen(url) as response:
xml = response.read()
url = response.geturl()
info = response.info()
# urldecode
print(urllib.parse.unquote(url))
# http://geocode.csis.u-tokyo.ac.jp/cgi-bin/simple_geocode.cgi?charset=UTF8&addr=池尻4-35-25
print(info)
# Date: Mon, 13 Jun 2022 15:33:19 GMT
# Server: Apache/2.4.41 (Ubuntu)
# Access-Control-Allow-Origin: *
# Vary: Accept-Encoding
# Connection: close
# Transfer-Encoding: chunked
# Content-Type: application/xml; charset=utf-8
root = etree.XML(xml)
children = list(root)
for elem in children:
print(elem.tag, elem.text)
for items in elem:
print(items.tag, items.text)
# query 池尻4-35-25
# geodetic wgs1984
# iConf 4
# converted 池尻4-35-
# candidate
# address 東京都/世田谷区/池尻/四丁目/35番
# longitude 139.673965
# latitude 35.654259
# iLvl 7
# candidate
# address 兵庫県/伊丹市/池尻/四丁目/35番地
# longitude 135.381805
# latitude 34.784988
# iLvl 7
参考にしたのは以下のサイト
Excel の WEBSERVICE 関数でジオコーディング
xml.etree.ElementTree --- ElementTree XML API
文字列を URL エンコード/デコードする (quote, unquote)
Windows 10 + Excel2016_x86 VBA で XML の基本的なことを少し触ってみる
xml.etree.ElementTree で XML の基本的なことを少し触ってみる
lxml.etree で XML の基本的なことを少し触ってみる