簡體   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-2024 STACKOOM.COM