<jupyter_start><jupyter_text>Requests/JSON Tutorial We first start by importing pprint which prints JSON nicely and requests which is a package for working with HTTP.<jupyter_code>import pprint
import requests<jupyter_output><empty_output><jupyter_text>Now we want a function that builds a URL, calls metaweather's API with that URL, and returns to us the JSON data of the response.<jupyter_code>def get_json_response(woeid):
    '''Gets forecast from metaweather.com in JSON format,
    whereonearthid is taken as an argument to specify forecast location.
    '''
    url = 'https://www.metaweather.com/api/location/{}'.format(woeid)
    response = requests.get(url)
    json_response = response.json()
    return json_response<jupyter_output><empty_output><jupyter_text>Set a variable equal to the get_json_response function and pass the where on earth id for the Seattle area as an argument. You can pprint this variable to get an idea of the structure of the JSON response we are dealing with.<jupyter_code>json_response = get_json_response(woeid=2490383)
pprint.pprint(json_response)<jupyter_output><empty_output><jupyter_text>We can make a function that gets the temperature forecast from our JSON response. This function accesses the value for 'consolidated_weather', which is a list and uses a for loop to iterate through that list. Note that 'the_temp' value is a float so we need to convert it to a string for printing via str(). See https://www.json.org/ for more info on the structure of JSON.<jupyter_code>def get_temp_forecast(json_response):
    '''Processes returned JSON from the get_json_response function for the short term temperature forecast.
    '''
    for item in json_response['consolidated_weather']:
        print('The forecast for ' + item['applicable_date'] + ' is: ' + str(item['the_temp']) + ' deg C')<jupyter_output><empty_output><jupyter_text>Call the get_temp_forecast function and pass json_response as an argument.<jupyter_code>get_temp_forecast(json_response)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07449432659102122, 'fraction_numerical': 0.003453379378391712, 'mean_word_length': 5.584415584415584, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '580036', 'license': 'MIT', 'n_tokens_mistral': 596, 'n_tokens_neox': 578, 'n_words': 217, 'path': 'metaweather_example .ipynb', 'repo_name': 'cougpy212/Mini-coding-challenge-getting-web-data', 'size': '2027'}