gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:05 25 4
gpt4 key购买 nike

我有一个 .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 时,requests 将它变成一个 url 查询字符串,该字符串编码不正确并且无论如何都不是 api 想要的。在我看来这很烦人。查询字符串和正文是不同的野兽 - 它们不应被压缩成单个参数并自动进行猜测。

这个有效:

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

关于python - 尝试使用 Python 请求将 xml 文件发送到 OpenStreetMap Overpass API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com