gpt4 book ai didi

python - 从 Web 响应中解析 Xml

转载 作者:数据小太阳 更新时间:2023-10-29 01:51:01 26 4
gpt4 key购买 nike

我正在尝试从 nominatim 那里获得对数千个城市进行地理编码的响应。

import os
import requests
import xml.etree.ElementTree as ET

txt = open('input.txt', 'r').readlines()
for line in txt:
lp, region, district, municipality, city = line.split('\t')
baseUrl = 'http://nominatim.openstreetmap.org/search/gb/'+region+'/'+district+'/'+municipality+'/'+city+'/?format=xml'
# eg. http://nominatim.openstreetmap.org/search/pl/podkarpackie/stalowowolski/Bojan%C3%B3w/Zapu%C5%9Bcie/?format=xml
resp = requests.get(baseUrl)
resp.encoding = 'UTF-8' # special diacritics
msg = resp.text
# parse response to get lat & long
tree = ET.parse(msg)
root = tree.getroot()
print tree

但结果是:

Traceback (most recent call last):
File "geo_miasta.py", line 17, in <module>
tree = ET.parse(msg)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1182, in parse
tree.parse(source, parser)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 647, in parse
source = open(source, "rb")
IOError: [Errno 2] No such file or directory: u'<?xml version="1.0" encoding="UTF-8" ?>\n<searchresults timestamp=\'Tue, 11 Feb 14 21:13:50 +0000\' attribution=\'Data \xa9 OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright\' querystring=\'\u015awierczyna, Drzewica, opoczy\u0144ski, \u0142\xf3dzkie, gb\' polygon=\'false\' more_url=\'http://nominatim.openstreetmap.org/search?format=xml&amp;exclude_place_ids=&amp;q=%C5%9Awierczyna%2C+Drzewica%2C+opoczy%C5%84ski%2C+%C5%82%C3%B3dzkie%2C+gb\'>\n</searchresults>'

这是怎么回事?

编辑:感谢@rob 我的解决方案是:

#! /usr/bin/env python2.7
# -*- coding: utf-8 -*-

import os
import requests
import xml.etree.ElementTree as ET

txt = open('input.txt', 'r').read().split('\n')

for line in txt:
lp, region, district, municipality, city = line.split('\t')
baseUrl = 'http://nominatim.openstreetmap.org/search/pl/'+region+'/'+district+'/'+municipality+'/'+city+'/?format=xml'
resp = requests.get(baseUrl)
msg = resp.content
tree = ET.fromstring(msg)
for place in tree.findall('place'):
location = '{:5f}\t{:5f}'.format(
float(place.get('lat')),
float(place.get('lon')))

f = open('result.txt', 'a')
f.write(location+'\t'+region+'\t'+district+'\t'+municipality+'\t'+city)
f.close()

最佳答案

您正在使用 xml.etree.ElementTree.parse() ,它以文件名或文件对象作为参数。但是,您传递的不是文件或文件对象,而是一个 unicode 字符串。

尝试 xml.etree.ElementTree.fromstring(text) .

像这样:

 tree = ET.fromstring(msg)

这是一个完整的示例程序:

import os
import requests
import xml.etree.ElementTree as ET

baseUrl = 'http://nominatim.openstreetmap.org/search/pl/podkarpackie/stalowowolski/Bojan%C3%B3w/Zapu%C5%9Bcie\n/?format=xml'
resp = requests.get(baseUrl)
msg = resp.content
tree = ET.fromstring(msg)
for place in tree.findall('place'):
print u'{:s}: {:+.2f}, {:+.2f}'.format(
place.get('display_name'),
float(place.get('lon')),
float(place.get('lat'))).encode('utf-8')

关于python - 从 Web 响应中解析 Xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com