use*_*726 1 python openstreetmap
我有一个 .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>
Run Code Online (Sandbox Code Playgroud)
我现在尝试(但失败)要做的就是通过 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
Run Code Online (Sandbox Code Playgroud)
但收到以下错误消息:
<?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>
Run Code Online (Sandbox Code Playgroud)
这表明请求成功到达 Overpass API 并取回一个 xml 文件,但似乎 xml 请求没有成功传输。我尝试了一些变化,但没有比这更好的了。显然,我对 Python 并不多......
您希望 xml 成为 POST 的正文。当您传入 dict 时, requests 会将其转换为未正确编码的 url 查询字符串,并且无论如何都不是 api 想要的。在我看来它非常烦人。查询字符串和主体是不同的野兽 - 它们不应该被简化为单个参数并自动猜测。
这有效:
import requests
r = requests.post('http://overpass-api.de/api/interpreter',
data=open('Bethesda.xml', 'rb'))
print(r.text)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3511 次 |
| 最近记录: |