- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Shane Lynn 上发现了一组非常有用的脚本 Analysis of Weather data 。第一个脚本用于从 Weather Underground 中抓取数据,如下所示:
import requests
import pandas as pd
from dateutil import parser, rrule
from datetime import datetime, time, date
import time
def getRainfallData(station, day, month, year):
"""
Function to return a data frame of minute-level weather data for a single Wunderground PWS station.
Args:
station (string): Station code from the Wunderground website
day (int): Day of month for which data is requested
month (int): Month for which data is requested
year (int): Year for which data is requested
Returns:
Pandas Dataframe with weather data for specified station and date.
"""
url = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID={station}&day={day}&month={month}&year={year}&graphspan=day&format=1"
full_url = url.format(station=station, day=day, month=month, year=year)
# Request data from wunderground data
response = requests.get(full_url, headers={'User-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'})
data = response.text
# remove the excess <br> from the text data
data = data.replace('<br>', '')
# Convert to pandas dataframe (fails if issues with weather station)
try:
dataframe = pd.read_csv(io.StringIO(data), index_col=False)
dataframe['station'] = station
except Exception as e:
print("Issue with date: {}-{}-{} for station {}".format(day,month,year, station))
return None
return dataframe
# Generate a list of all of the dates we want data for
start_date = "2016-08-01"
end_date = "2016-08-31"
start = parser.parse(start_date)
end = parser.parse(end_date)
dates = list(rrule.rrule(rrule.DAILY, dtstart=start, until=end))
# Create a list of stations here to download data for
stations = ["ILONDON28"]
# Set a backoff time in seconds if a request fails
backoff_time = 10
data = {}
# Gather data for each station in turn and save to CSV.
for station in stations:
print("Working on {}".format(station))
data[station] = []
for date in dates:
# Print period status update messages
if date.day % 10 == 0:
print("Working on date: {} for station {}".format(date, station))
done = False
while done == False:
try:
weather_data = getRainfallData(station, date.day, date.month, date.year)
done = True
except ConnectionError as e:
# May get rate limited by Wunderground.com, backoff if so.
print("Got connection error on {}".format(date))
print("Will retry in {} seconds".format(backoff_time))
time.sleep(10)
# Add each processed date to the overall data
data[station].append(weather_data)
# Finally combine all of the individual days and output to CSV for analysis.
pd.concat(data[station]).to_csv("data/{}_weather.csv".format(station))
但是,我收到错误:
Working on ILONDONL28
Issue with date: 1-8-2016 for station ILONDONL28
Issue with date: 2-8-2016 for station ILONDONL28
Issue with date: 3-8-2016 for station ILONDONL28
Issue with date: 4-8-2016 for station ILONDONL28
Issue with date: 5-8-2016 for station ILONDONL28
Issue with date: 6-8-2016 for station ILONDONL28
谁能帮我解决这个错误吗?
所选站点和时间段的数据可用,如link所示.
最佳答案
您获得的输出是因为引发了异常。如果您添加了 print e
,您会发现这是因为脚本顶部缺少 import io
。其次,你给的站名少了一个字。请尝试以下操作:
import io
import requests
import pandas as pd
from dateutil import parser, rrule
from datetime import datetime, time, date
import time
def getRainfallData(station, day, month, year):
"""
Function to return a data frame of minute-level weather data for a single Wunderground PWS station.
Args:
station (string): Station code from the Wunderground website
day (int): Day of month for which data is requested
month (int): Month for which data is requested
year (int): Year for which data is requested
Returns:
Pandas Dataframe with weather data for specified station and date.
"""
url = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID={station}&day={day}&month={month}&year={year}&graphspan=day&format=1"
full_url = url.format(station=station, day=day, month=month, year=year)
# Request data from wunderground data
response = requests.get(full_url)
data = response.text
# remove the excess <br> from the text data
data = data.replace('<br>', '')
# Convert to pandas dataframe (fails if issues with weather station)
try:
dataframe = pd.read_csv(io.StringIO(data), index_col=False)
dataframe['station'] = station
except Exception as e:
print("Issue with date: {}-{}-{} for station {}".format(day,month,year, station))
return None
return dataframe
# Generate a list of all of the dates we want data for
start_date = "2016-08-01"
end_date = "2016-08-31"
start = parser.parse(start_date)
end = parser.parse(end_date)
dates = list(rrule.rrule(rrule.DAILY, dtstart=start, until=end))
# Create a list of stations here to download data for
stations = ["ILONDONL28"]
# Set a backoff time in seconds if a request fails
backoff_time = 10
data = {}
# Gather data for each station in turn and save to CSV.
for station in stations:
print("Working on {}".format(station))
data[station] = []
for date in dates:
# Print period status update messages
if date.day % 10 == 0:
print("Working on date: {} for station {}".format(date, station))
done = False
while done == False:
try:
weather_data = getRainfallData(station, date.day, date.month, date.year)
done = True
except ConnectionError as e:
# May get rate limited by Wunderground.com, backoff if so.
print("Got connection error on {}".format(date))
print("Will retry in {} seconds".format(backoff_time))
time.sleep(10)
# Add each processed date to the overall data
data[station].append(weather_data)
# Finally combine all of the individual days and output to CSV for analysis.
pd.concat(data[station]).to_csv(r"data/{}_weather.csv".format(station))
为您提供一个输出 CSV 文件,开头如下:
,Time,TemperatureC,DewpointC,PressurehPa,WindDirection,WindDirectionDegrees,WindSpeedKMH,WindSpeedGustKMH,Humidity,HourlyPrecipMM,Conditions,Clouds,dailyrainMM,SoftwareType,DateUTC,station
0,2016-08-01 00:05:00,17.8,11.6,1017.5,ESE,120,0.0,0.0,67,0.0,,,0.0,WeatherCatV2.31B93,2016-07-31 23:05:00,ILONDONL28
1,2016-08-01 00:20:00,17.7,11.0,1017.5,SE,141,0.0,0.0,65,0.0,,,0.0,WeatherCatV2.31B93,2016-07-31 23:20:00,ILONDONL28
2,2016-08-01 00:35:00,17.5,10.8,1017.5,South,174,0.0,0.0,65,0.0,,,0.0,WeatherCatV2.31B93,2016-07-31 23:35:00,ILONDONL28
如果您没有获得 CSV 文件,我建议您添加输出文件名的完整路径。
关于python - 使用 pandas 从 wunderground 中抓取天气数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39892710/
有谁知道在给定顶点列表的情况下以编程方式创建冷/暖锋矢量的方法(注意顶点不包含小三 Angular 形或半圆;[-105W,40.45N]等...)? 我正在尝试在网络 map 应用程序上放置“当前前
在 iOS 天气应用程序的右下角,有一个表格/列表图标。我想知道这是一个栏按钮项目,还是一个自定义图标。 最佳答案 这是一个带有自定义图标的自定义按钮。要查找来自 Apple 的所有模板图标,请转到
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。为了帮助澄清这个问题以便可以重新打开它,visit the help center
很多用户为了提高自己的工作效率,都会在电脑桌面上添加日历,天气,时钟等小工具,防止忘记一些重要的事项。那如何在win10系统桌面添加日历,天气,时钟等小工具呢?下面就跟着小编一起来看看吧。 Win
我试图使用 NOAA 进行每小时预报,但它不允许我输入东经。请求必须是西经。 他们甚至为印度等亚洲国家提供东经服务。 这是我使用的链接 http://graphical.weather.gov/xml
我正在使用 pywapi 来获取不同地点的天气状况。我使用的命令是: alaska = pywapi.get_weather_from_noaa('PABI') 其中“PABI”是阿拉斯加的电台 ID
我目前正在使用 xCode 开发一个应用程序,我是 iOS 新手。我的天气格式当前正在向我的数据库提交“摄氏度”和“华氏度”,而不是发送 C 或 F。我已经尝试了几件事,但看起来它也将我的显示文本更改
我在一些地方看到人们正在寻找天气应用程序效果。这些问题中有很多是针对 Android 应用程序或 JAVA 的,但我正在寻找在 HTML/CSS/Javascript 网站上的使用。这可能吗? 当您查
我正在使用雅虎! Weather RSS Feed 以获取我所在城镇的预报。我可以解析 XML 并获取天气描述,但随后我得到如下结果: Current Conditions: Light Rain,
这个问题已经有答案了: How can I access and process nested objects, arrays, or JSON? (31 个回答) 已关闭 6 年前。 所以,我正在尝
我正在使用 SAX 从 google 天气 API 中提取信息,但我遇到了“条件”问题。 具体来说,我正在使用这段代码: public void startElement (String uri, S
我正在使用一个简单的 Web API 来获取伦敦城市的天气详细信息。但是,我没有得到所需的响应,而是收到 401 错误。我做错了什么吗? HTML $(document).ready(func
我正在使用 Open Weather API,但是当我发送城市数据请求时,出现网络错误。 这是获取资源的操作。 actions/index.js import axios from "axios";
这段代码中的所有内容我都在尝试为用户提供本地的位置和温度但是不知何故温度以摄氏度显示的方式更少而且下面也没有更新是我尝试过的就像它是 4-5 小时回溯数据到 10 度摄氏度不太像如果温度是 22(摄氏
我正在尝试使用 CLLocation 来捕获经度和纬度,然后使用 Alamofire 中的经度和纬度来获取天气。每次,经度和纬度都不会停止更新,也不会打印天气数据(如果你想查看这里是数据的示例链接:h
fullfillment page至于我个人的兴趣,我想使用天气 api 制作一个天气聊天机器人。为此,我使用对话流,但是当我按照 Dialogflow github 进行 webhook 连接时但出
我正在开发一款应用程序,我相信它会对学生有所帮助。我想根据位置包括天气(仅温度)。例如,如果我要从一个城市前往另一个城市,我希望它能针对新城市自动更新。我该怎么做?对于我的第二个问题,我还想将日期(即
有谁知道如何制作像天气/新闻应用程序中的 Nexus One 选项卡这样的选项卡,我的意思是通过轻弹屏幕即可转到下一个选项卡的功能,谢谢 视频:http://www.youtube.com/watch
我正在尝试制作一个天气应用程序。到目前为止,我设法从 rss 中获得了一些基本信息。我正在使用以下代码: if ([currentElement isEqualToString:@"descr
我有一个由站点标识符代码(“usaf”)和日期组织的表面天气观测数据框(fzraHrObs)。 fzraHrObs 有几列天气数据。车站代码和日期(日期时间对象)如下所示: usaf dat
我是一名优秀的程序员,十分优秀!