Dan Grist
Dan Grist

Reputation: 11

Python Requests, download Overpass Turbo XML

Any help for the following issue would be greatly appreciated.

I am attempting to use requests to fill in the overpass API query form found here http://overpass-api.de/query_form.html/.

The manual code I would type in the query box is as follows.

<union>
<bbox-query s="48.657" w="2.18" n="49.063" e="2.605"/>
<recurse type="up"/>
</union>
<print mode="meta"/>

This will result in an OSM XML file being downloaded to my downloads folder.

For requests I am attempting the following.

import requests

url = r"http://overpass-api.de/query_form.html"

mydata = """<union> 
<bbox-query s="48.657" w="2.18" n="49.063" e="2.605"/> 
<recurse type="up"/>
</union>
<print mode="meta"/>"""

x = requests.post(url, data = mydata) ## or i have also tried x = requests.post(url, {data : mydata})

print(x.text)

This just returns the HTML of the website that is visible using control + U, the method is Post, and the data field is called "data" so as far as i am aware, this should result in the download starting. I have limited experience with requests, so any help would be greatly appreciated.

Thanks

Upvotes: 1

Views: 1301

Answers (1)

Parfait
Parfait

Reputation: 107587

Simply adjust the URL to include the post form's action redirect:

url = r"http://overpass-api.de/api/interpreter" 

mydata = '<union> <bbox-query s="48.657" w="2.18" n="49.063" e="2.605"/> <recurse type="up"/> </union>'

x = requests.post(url, data = mydata)

print(x.text)
# <?xml version="1.0" encoding="UTF-8"?>
# <osm version="0.6" generator="Overpass API 0.7.56.1004 6cd3eaec">
# <note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
# <meta osm_base="2020-07-04T03:47:02Z"/>

Upvotes: 2

Related Questions