Apr-15-2021, 09:33 AM
When I am using the api, the data is coming in json format and I want to save all this data in dataframe.
import json import requests hostname = "http://geomag.bgs.ac.uk/web_service/GMModels/igrf/13/?latitude=-35&longitude=149&altitude=0&date=2021-03-23&format=json" try: response = requests.get(hostname) # extract JSON payload of response as Python dictionary json_payload = response.json() # raise an Exception if we encoutnered any HTTP error codes like 404 response.raise_for_status() except requests.exceptions.ConnectionError as e: # handle any typo errors in url or endpoint, or just patchy internet connection print(e) except requests.exceptions.HTTPError as e: # handle HTTP error codes in the response print(e, json_payload['error']) except requests.exceptions.RequestException as e: # general error handling print(e, json_payload['error']) else: json_payload = response.json() print(json.dumps(json_payload, indent=4, sort_keys=True))