Test JSON File
Copy onto your test web site for initial testing.
{
"name": "Billy Bob",
"title": "Test Json File",
"date": "20240504",
"list": [1,2,3,4,5]
}
Then create your own JSON data for testing.
(Be sure to use double quotes. Single quotes cause errors.)
Non-Cached Web Request
#!/usr/bin/python3
# ==============================================================
# pip install requests
# pip install html-to-json
# ==============================================================
import requests
import html_to_json
from pprint import pprint
URL: Final = 'https://w3schools.com/python/demopage.htm'
# ---------------------------------------------------------------
# ---- get response from URL (no caching)
# ---------------------------------------------------------------
def get_response():
try:
response = requests.get(URL)
print(f'URL: {URL}')
print(f'response type {type(response)}')
print(f'apparent encoding {response.apparent_encoding}')
print(f'encoding {response.encoding}')
content = response.content
print()
print(type(content))
print(f'---- content\n{content}')
string = content.decode('utf-8')
print()
print(type(string))
print(f'---- string\n{string}')
json: dict = html_to_json.convert(string)
print()
print(type(json))
print(f'---- json\n{json}')
print()
pprint(json)
except Exception as e:
print(f'Error status code {response.status_code}')
print(e)
# ---------------------------------------------------------------
# ---- main
# ---------------------------------------------------------------
if __name__ == '__main__':
get_response()
Cached Web Request
#!/usr/bin/python3
# ================================================================
# Based on: www.youtube.com/watch?v=cJBYGSXcCgQ&t=172s
# "OPTIMIZE" Your Python Apps By Caching
# ================================================================
from typing import Final
from dataclasses import dataclass
from requests_cache import CachedSession
from pprint import pprint
URL: Final = 'https://www.???????.com/my_test.json'
@dataclass
class MyTest:
name: str = None
title: str = None
date: str = None
list: list = None
# -----------------------------------------------------------------
# ---- get cached response from URL
# -----------------------------------------------------------------
def get_response():
try:
# ---- cache session
session = CachedSession(
cache_name='cache/my_test', # cache folder and name
expire_after=60) # optional cache
# expiration time
# 60 sec = 1 min
response = session.get(URL)
json = response.json()
mytest = MyTest(**json)
print(f'"{mytest.name}"')
print(f'"{mytest.list}"')
print(f'"{mytest.title}"')
except Exception as e:
print(f'Exception returned status code {response.status_code}')
print(e)
# --------------------------------------------------------------------
# ---- main
# --------------------------------------------------------------------
if __name__ == '__main__':
get_response()
Non-cached Web API Request
#!/usr/bin/python3
# ==========================================================
# from: www.geeksforgeeks.org/response-json-python-requests/
# (returns JSON Data)
# ==========================================================
import requests
from pprint import pprint
# ---- get request
# ---- (web site has API interface - returns json data)
URL = 'https://api.github.com'
response = requests.get(URL)
# ---- print response content (bytes string)
##print()
##print(response.content)
# ---- get response as json dictionary
json: dict = response.json()
# ----print json content
print()
pprint(json)