我有一个 .xml 请求,可以成功地从 OpenStreetMap Overpass API 检索数据。
<?xml version="1.0" encoding="UTF-8"?>
<osm-script>
<query type="node">
<has-kv k="name" v="Bethesda"/>
<has-kv k="network" v="Washington Metro"/>
</query>
<query type="way">
<around radius="800"/>
<has-kv k="building"/>
</query>
<union>
<item/>
<recurse type="down"/>
</union>
<print/>
</osm-script>
我现在正在尝试(但失败)要做的就是通过 Python 请求库发送这个 xml(我对其他 Python 解决方案持开放态度)。我发送以下请求:
files = {'file': ('Bethesda.xml', open('Bethesda.xml', 'rb'))}
r = requests.post('http://overpass-api.de/api/interpreter', data=files)
print r.text
但收到以下错误消息:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en"/>
<title>OSM3S Response</title>
</head>
<body>
<p>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</p>
<p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Unknown type "file" </p>
<p><strong style="color:#FF0000">Error</strong>: line 1: parse error: An empty query is not allowed </p>
<p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Unknown type "=" </p>
<p><strong style="color:#FF0000">Error</strong>: line 1: parse error: An empty query is not allowed </p>
<p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Unknown type "Bethesda" </p>
<p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ';' expected - '&' found. </p>
<p><strong style="color:#FF0000">Error</strong>: line 2: parse error: Unexpected end of input. </p>
这表明请求成功到达 Overpass API 并返回一个 xml 文件,但似乎 xml 请求没有成功传输。我尝试了一些变化,但没有比这更好的了。显然,我对 Python 不太了解……