Exception has occurred: JSONDecodeError Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred:

import urllib.request
import json

def printResults(data):
    
    theJSON = json.loads(data)
    
    if "title" in theJSON["metadata"]:
        print(theJSON["metadata"]["title"])
    
    count = theJSON["metadata"]["count"]
    print(str(count) + "events recorded") 
    
    

def main():
    
    UrlData = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php"
    # open url and read data
    webUrl = urllib.request.urlopen(UrlData)
    print("result code: " + str(webUrl.getcode()))
    if(webUrl.getcode() == 200):
        data = webUrl.read()
        printResults(data)
    else:
        print("Received error, cannot parse results")


if __name__ == "__main__":
    main()

I am receiving the error above in the title. I seem to have no idea why I am keep getting this error despite doing everything in the lecture right.

Answer

The code below works (your code does not work since the url you use does NOT point to a json doc)

import requests
import pprint

r = requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson')
if r.status_code == 200:
    pprint.pprint(r.json())

output

{'features': [],
 'metadata': {'api': '1.10.3',
              'count': 0,
              'generated': 1631451490000,
              'status': 200,
              'title': 'USGS Significant Earthquakes, Past Day',
              'url': 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson'},
 'type': 'FeatureCollection'}