【问题标题】:Trying to send xml file using Python Requests to OpenStreetMap Overpass API尝试使用 Python 请求将 xml 文件发送到 OpenStreetMap Overpass API
【发布时间】:2013-05-18 14:52:29
【问题描述】:

我有一个 .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 不太了解……

【问题讨论】:

    标签: python openstreetmap


    【解决方案1】:

    您希望 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      相关资源
      最近更新 更多