A Python example of how to get a JSON value from the API
I'm learning Python and used the Clicky API as a small project to get todays visitors. It's very basic but it does the job. Just wanted to share it and maybe it helps someone to get started ;-)
I use REQUESTS for the http handeling and JSON decoding. You can install it by using: pip install requests
Have fun!
Rick
# code starts below
import requests
# Set the request parameters
url = 'https://api.clicky.com/api/stats/4?site_id=YOUR_SITE_ID&sitekey=YOUR_SITE_KEY&type=visitors&output=json'
# Fetch url
print("Fetching url..")
# Do the HTTP get request
response = requests.get(url, verify=True) #Verify is check SSL certificate
# Error handling
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Problem with the request. Exiting.')
exit()
# Decode the JSON response into a dictionary and use the data
data = response.json()
visitors = data[0]['dates'][0]['items'][0]['value']
print('nVisitors today:'), visitors
# code ends
Posted Thu Jan 22 2015 4:05a by rhoekm***
You must be
logged in to your account to post!