Home > Enterprise >  Web scraping national weather API difficulty getting correct format in Python
Web scraping national weather API difficulty getting correct format in Python

Time:09-18

I am attempting to scrape data from this link "https://api.weather.gov/gridpoints/MFL/44,66" and get it into the correct format. This is the challenge:

In this assignment, you should write a function (named gets_temperature_forecast()) that gets an address/city and returns temperatures forecasts (in Centigrade and Fahrenheit) from the National Weather Service. The function output should be like the following:

****Date: 2019-07-04 Time: 08:00:00 Temperature: 27.22 C (81.0 F)**** #with the little circle on top etc

it looks like this before trying to format

{'validTime': '2021-09-11T21:00:00 00:00/PT2H', 'value': 29.444444444444443}, {'validTime': '2021-09-11T23:00:00 00:00/PT1H', 'value': 28.333333333333332}, 

I have researched many of the question in this platform and still having trouble. I cant seem to get the data to format correctly, when I try to extract the time, I am not successful in converting it to regular time, plus my last line of code is not working. I am thinking I may need to use the datetime function to strip the date, but then I cant convert it. Same for the Celsius to get the C and F formatted correctly.

Any help would be greatly appreciated!

here is my code:

import datetime
import pandas as pd
import requests
import json

url = "https://api.weather.gov/gridpoints/MFL/44,66"

response = requests.get(url).json()
print(response) #checking to see i have the URL

data_cleaned = []

for time in response ['data']['values']: #here is where everything gets messed up 
    data_cleaned.append({
       'Date': time['validTime'],
       'Time': (time['T']),
       'Temperature': time['value'], (time['value']*1.8)   32
        })
 

CodePudding user response:

Try this:

gets_temperature_forecast(address, city):

    url = 'https://api.weather.gov/gridpoints/MFL/44,66'
    response = requests.get(url).json()
    
    time = response['temperature']['values'][0]['validTime']
    temp = response['temperature']['values'][0]['value']
    
    print(f"Temperature: {temp}\nTime: {time}")

I hope this helps get you on the right track, though you would need to figure out how to get these stats using the address/city by checking the API docs on your own. ( I understand that you need to append the dicts to a list, but I think the example I've shown you will hopefully be enough to help you figure out the rest on your own )

  • Related