Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Python to display content from URL

Tags:

python

I am brand new to Python coding, and getting stuck with a particular task.

I have the following URL: https://freeman7.zendesk.com/api/v2/views/313117127/count.json

I would like to use Python to display something like the following (this is the content that that URL displays when you open it in a web browser):

{
"view_count": {
"url":     "https://freeman7.zendesk.com/api/v2/views/313117127/count.json",
"view_id": 313117127,
"value":   2,
"pretty":  "2",
"fresh":   true
}
}

I've tried all sorts of things after searching the Internet for a couple of hours, but can't seem to find anything that works. Here is an example of something I've tried:

import urllib.request, json
with urllib.request.urlopen("url") as url:
    data = json.loads(url.read().decode())
    print(data)

And here is the error I get for that:

URLError: urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Anyone have any idea what's going on here, and/or have a recommendation on how I could successfully complete this task? I would appreciate any insight!

Thanks, Stephen

like image 462
Stephen Freeman Avatar asked Nov 08 '22 22:11

Stephen Freeman


1 Answers

This works:

import json

import requests

url = 'https://freeman7.zendesk.com/api/v2/views/313117127/count.json'
response = requests.get(url)
response_json = response.json()
print(json.dumps(response_json, sort_keys=True, indent=4))

Results:

{
    "error": {
        "message": "There is no help desk configured at this address. This means that the address is available and that you can claim it at http://www.zendesk.com/signup",
        "title": "No help desk at freeman7.zendesk.com"
    }
}

If I had access to the actual content it would be better, but this will work for all JSON responses from a URL.

Also, for viewing JSON content in a browser, this Chrome extension is clutch: https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa

like image 53
Utkonos Avatar answered Nov 14 '22 21:11

Utkonos