所有,

我正在尝试从Wunderground下载天气数据历史记录。我的问题是我需要完整的METAR信息。

这是我要下载的示例:CSV with full METAR

由于我要下载全年的每小时数据,因此需要编写脚本。但是,无论我尝试了什么(使用wget或python进行重击),我仍然无法通过脚本获得包含完整METAR的页面。

这是我的脚本示例:

import urllib2
from BeautifulSoup import BeautifulSoup
url = "http://www.wunderground.com/history/airport/KBUF/2011/1/1/DailyHistory.html?theprefset=SHOWMETAR&theprefvalue=1&format=1"
page = urllib2.urlopen(url)
dailyData = page.read()
print dailyData


我所拥有的是这样的:

12:54 AM,52.0,45.0,77,29.93,10.0,SSW,15.0,-,N/A,,Scattered Clouds,200,2011-01-01 05:54:00<br />
1:54 AM,53.1,45.0,74,29.95,10.0,SSW,12.7,-,N/A,,Mostly Cloudy,200,2011-01-01 06:54:00<br />
2:54 AM,50.0,44.1,80,29.95,10.0,SSW,8.1,-,N/A,,Mostly Cloudy,200,2011-01-01 07:54:00<br />
3:54 AM,51.1,44.1,77,29.93,10.0,SSE,5.8,-,N/A,,Scattered Clouds,150,2011-01-01 08:54:00<br />


通过网络浏览器,这就是我所得到的-请注意以METAR开头的新列:

12:54 AM,52.0,45.0,77,29.93,10.0,SSW,15.0,-,N/A,,Scattered Clouds,METAR KBUF 010554Z COR 20013KT 10SM FEW045 SCT140 11/07 A2992 RMK AO2 SLP134 60004 T01110072 10111 20078 58016,200,2011-01-01 05:54:00
1:54 AM,53.1,45.0,74,29.95,10.0,SSW,12.7,-,N/A,,Mostly Cloudy,METAR KBUF 010654Z 20011KT 10SM BKN055 BKN130 12/07 A2994 RMK AO2 SLP141 T01170072,200,2011-01-01 06:54:00
2:54 AM,50.0,44.1,80,29.95,10.0,SSW,8.1,-,N/A,,Mostly Cloudy,METAR KBUF 010754Z 20007KT 10SM BKN050 BKN130 10/07 A2994 RMK AO2 SLP140 T01000067,200,2011-01-01 07:54:00
3:54 AM,51.1,44.1,77,29.93,10.0,SSE,5.8,-,N/A,,Scattered Clouds,METAR KBUF 010854Z 15005KT 10SM SCT050 SCT130 11/07 A2992 RMK AO2 SLP134 T01060067 58000,150,2011-01-01 08:54:00


任何对此的解决方案将不胜感激。谢谢!

最佳答案

Browsing the wunderunderground,我找到了"Show full METARS"链接。单击此处后,将浏览器指向link you posted"Comma Delimited File" link将显示METAR数据。它似乎设置了一些cookie。例如,page.info()显示“ Prefs”包括“ SHOWMETAR:1”:

Set-Cookie: Prefs=FAVS:1|WXSN:1|PWSOBS:1|WPHO:1|PHOT:1|RADC:0|RADALL:0|HIST0:NULL|GIFT:1|SHOWMETAR:1|PHOTOTHUMBS:50|HISTICAO:KBUF*NULL|; path=/; expires=Fri, 01-Jan-2020 00:00:00 GMT; domain=.wunderground.com




import urllib2
import cookielib

cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))

setmetar = 'http://www.wunderground.com/cgi-bin/findweather/getForecast?setpref=SHOWMETAR&value=1'
request = urllib2.Request(setmetar)
response = opener.open(request)

url = "http://www.wunderground.com/history/airport/KBUF/2011/1/1/DailyHistory.html?theprefset=SHOWMETAR&theprefvalue=1&format=1"
request = urllib2.Request(url)
page = opener.open(request)
# print(page.info())
dailyData = page.read()
print dailyData


产量

TimeEST,TemperatureF,Dew PointF,Humidity,Sea Level PressureIn,VisibilityMPH,Wind Direction,Wind SpeedMPH,Gust SpeedMPH,PrecipitationIn,Events,Conditions,FullMetar,WindDirDegrees,DateUTC<br />
12:54 AM,52.0,45.0,77,29.93,10.0,SSW,15.0,-,N/A,,Scattered Clouds,METAR KBUF 010554Z COR 20013KT 10SM FEW045 SCT140 11/07 A2992 RMK AO2 SLP134 60004 T01110072 10111 20078 58016,200,2011-01-01 05:54:00<br />
1:54 AM,53.1,45.0,74,29.95,10.0,SSW,12.7,-,N/A,,Mostly Cloudy,METAR KBUF 010654Z 20011KT 10SM BKN055 BKN130 12/07 A2994 RMK AO2 SLP141 T01170072,200,2011-01-01 06:54:00<br />

关于python - 从Wunderground解析HTML数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9201647/

10-12 15:30