Python Forum

Full Version: Querying a web database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good afternoon,

I have been playing around with Requests to Query an API. Here is my code

import json
import requests

data_url = 'http://sagisservices.thempc.org/saint/rest/services/OpenData/Coastal_LandCover/MapServer/4'

r = requests.get(data_url)

data = r.json()
In this case it returns an error which I guess I dont understand because if you put the link into the browser the featuredserver says that it supports JSON, AMF.

I've been trying to help my self by Googling "How to Query an API with Python" but I get a bunch of links for MYSQL and I don't think that's what I'm looking for. I would just like to learn what I am doing wrong or read about the basics of Querying an API with Requests() in Python.
It's okay,but you have the link that do not return JSON.
It's a JSON link in top left corner.
import json
import requests

data_url = 'http://sagisservices.thempc.org/saint/rest/services/OpenData/Coastal_LandCover/MapServer/4?f=pjson'
r = requests.get(data_url)
data = r.json()
It will now return a Python dictionary with also list inside.
So example to get data.
>> data['currentVersion']
10.22
>>> data['extent']
{'spatialReference': {'latestWkid': 26917, 'wkid': 26917},
 'xmax': 523470.3594000004,
 'xmin': 463040.45940000005,
 'ymax': 3566788.1104000006,
 'ymin': 3507761.0282000005}

>>> # To access a list use [0]
>>> data['drawingInfo']['renderer']['uniqueValueInfos'][0]
{'description': '',
 'label': 'Alluvial (Brownwater) Rivers and Swamps',
 'symbol': {'color': [68, 130, 99, 255],
            'outline': {'color': [110, 110, 110, 255],
                        'style': 'esriSLSSolid',
                        'type': 'esriSLS',
                        'width': 0.4},
            'style': 'esriSFSSolid',
            'type': 'esriSFS'},
 'value': 'Alluvial (Brownwater) Rivers and Swamps'}

# To get value in data over
>>> data['drawingInfo']['renderer']['uniqueValueInfos'][0]['value']
'Alluvial (Brownwater) Rivers and Swamps'
Hi,

I too have spent many hours struggling with API's. You may conclude that there is no rhyme or reason to how they work...... (ok there probably is but...)

If you look at the "header" for "r" then this may work for you. Try adding this line after the requests.get call.

 r = r.headers
The value of "r" should give you the response from the API call.

Let me know if this does not work or if you have difficulties with the next stage of the script.

Good Luck

Bass
(Aug-18-2017, 07:22 PM)snippsat Wrote: [ -> ]It's okay,but you have the link that do not return JSON.
It's a JSON link in top left corner.

Thank you, what is the difference between these two querys?

http://sagisservices.thempc.org/saint/re...r/4?f=json

http://sagisservices.thempc.org/saint/re.../4?f=pjson

One has format = pjson and the other has format = json what's the difference between those? EDIT: It looks like one of them returns the JSON() in pretty print!

Finally, is there a way to Query an ArcGIS server for its map contents? For instance I am part of a group which displays a bunch of information on an ArcGIS map and I have gone into the element of the webpage and looked at the XHR request the webpage made and then tried to request from the same server using requests() but I cannot figure out how to return the layer which has the arcgis layer information. For example say a layer of all Signs. That's what my end goal is for requesting from an API.

(Aug-18-2017, 07:24 PM)Bass Wrote: [ -> ]The value of "r" should give you the response from the API call.

Let me know if this does not work or if you have difficulties with the next stage of the script.

Thanks! How can I best take advantage of this information when using the requests() input? What does it tell me or how does it differ from one requests() to the next?