Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Querying a web database
#1
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.
Reply
#2
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'
Reply
#3
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

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#4
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,392 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Fastest Method for Querying SQL Server with Python Pandas BuJayBelvin 7 6,938 Aug-02-2020, 06:21 PM
Last Post: jefsummers
  Recommendations: Querying CSV / Database?? ironfelix717 2 1,664 Apr-01-2020, 12:13 PM
Last Post: ndc85430
  Text Entry and Querying CSV whacky7 4 3,043 Mar-28-2019, 11:19 AM
Last Post: whacky7
  Querying cvs excel files rturus 6 3,508 Mar-27-2019, 02:08 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020