网络抓取国家天气 API 难以在 Python 中获得正确的格式
Web scraping national weather API difficulty getting correct format in Python
我正在尝试从此 link“https://api.weather.gov/gridpoints/MFL/44,66”中抓取数据并将其转换为正确的格式。这就是挑战:
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
在尝试格式化之前看起来像这样
{'validTime': '2021-09-11T21:00:00+00:00/PT2H', 'value': 29.444444444444443}, {'validTime': '2021-09-11T23:00:00+00:00/PT1H', 'value': 28.333333333333332},
我已经研究了这个平台上的很多问题,但仍然遇到问题。我似乎无法正确格式化数据,当我尝试提取时间时,我没有成功将其转换为常规时间,而且我的最后一行代码也不起作用。我在想我可能需要使用 datetime 函数来去除日期,但我无法转换它。摄氏度相同,以正确格式化 C 和 F。
如有任何帮助,我们将不胜感激!
这是我的代码:
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
})
试试这个:
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}")
我希望这可以帮助您走上正轨,尽管您需要自己查看 API 文档来了解如何使用 address/city 获取这些统计数据。 (我知道您需要将指令附加到列表中,但我认为我向您展示的示例有望足以帮助您自行解决其余问题)
我正在尝试从此 link“https://api.weather.gov/gridpoints/MFL/44,66”中抓取数据并将其转换为正确的格式。这就是挑战:
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
在尝试格式化之前看起来像这样
{'validTime': '2021-09-11T21:00:00+00:00/PT2H', 'value': 29.444444444444443}, {'validTime': '2021-09-11T23:00:00+00:00/PT1H', 'value': 28.333333333333332},
我已经研究了这个平台上的很多问题,但仍然遇到问题。我似乎无法正确格式化数据,当我尝试提取时间时,我没有成功将其转换为常规时间,而且我的最后一行代码也不起作用。我在想我可能需要使用 datetime 函数来去除日期,但我无法转换它。摄氏度相同,以正确格式化 C 和 F。
如有任何帮助,我们将不胜感激!
这是我的代码:
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
})
试试这个:
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}")
我希望这可以帮助您走上正轨,尽管您需要自己查看 API 文档来了解如何使用 address/city 获取这些统计数据。 (我知道您需要将指令附加到列表中,但我认为我向您展示的示例有望足以帮助您自行解决其余问题)