繁体   English   中英

尝试使用Python请求将xml文件发送到OpenStreetMap Overpass API

[英]Trying to send xml file using Python Requests to OpenStreetMap Overpass API

我有一个.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并不多...

您希望xml成为POST的主体。 当您传入dict时,请求将其转换为未正确编码的url查询字符串,而不是api想要的。 在我看来它非常烦人。 查询字符串和正文是不同的野兽 - 它们不应该被压缩成单个参数并自动地估计。

这有效:

import requests
r = requests.post('http://overpass-api.de/api/interpreter',
    data=open('Bethesda.xml', 'rb'))
print(r.text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM