简体   繁体   English

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

[英]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.

相关问题 如何使用 Python 和立交桥 API 从 OpenStreetMap 下载非洲的所有医院? - How can I download all hospitals in Africa from OpenStreetMap using Python and the Overpass API? 从OpenStreetMap Overpass API输出中提取ID(字典中的Python列表) - Extract ids from OpenStreetMap Overpass API output (python list in dictionary) Python 请求,下载 Overpass Turbo XML - Python Requests, download Overpass Turbo XML Python包装器在Overpass-API的端点上运行请求 - Python wrapper to run requests on the endpoint of overpass-API 无法通过Python使用Overpass API打印查询 - Failing to print query using Overpass API with Python python OSM xml立交桥 - python OSM xml overpass 如何使用python从OpenStreetMap网站导出的.osm文件的相同结构中,使用python将OSM文件保存为XML? - How to save OSM file in XML using python in the same structure of the exported .osm file from OpenStreetMap website? 使用 python 请求通过发送 Api 参考上传/发送文件到 Facebook - Upload/Send file to Facebook via Send Api Reference using python requests 使用 Python 发送和接收文件:FastAPI 和请求 - Send and receive file using Python: FastAPI and requests 尝试使用请求库Python发送视频时发生错误 - error when trying to send a video using requests library Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM