[英]Trying to send xml file using Python Requests to OpenStreetMap Overpass API
I have an .xml request that can succesfully retrieve data from the 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>
All I'm trying (and failing) to do now is send this xml via the Python requests library (i'm open to other python solutions). 我现在正在尝试(并且失败)的是通过Python请求库发送这个xml(我对其他python解决方案开放)。 I send the request below:
我发送以下请求:
files = {'file': ('Bethesda.xml', open('Bethesda.xml', 'rb'))}
r = requests.post('http://overpass-api.de/api/interpreter', data=files)
print r.text
but get the following error message: 但得到以下错误消息:
<?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>
Which indicates the request successfully reaches the Overpass API and gets back an xml file, but it seems like the xml request wasn't successfully transferred. 这表示请求成功到达Overpass API并获取了一个xml文件,但似乎没有成功传输xml请求。 I've tried a few variations but can't do much better than this.
我尝试了一些变化,但不能比这更好。 Clearly, I'm not much with Python...
显然,我对Python并不多...
You want the xml to be the body of the POST. 您希望xml成为POST的主体。 When you pass in the dict, requests turns it into a url query string that isn't encoded correctly and isn't what the api wants anyway.
当您传入dict时,请求将其转换为未正确编码的url查询字符串,而不是api想要的。 Its very annoying in my opinion.
在我看来它非常烦人。 Query strings and bodies are different beasts - they shouldn't be smooshed into a single parameter and automagically guestimated.
查询字符串和正文是不同的野兽 - 它们不应该被压缩成单个参数并自动地估计。
This works: 这有效:
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.