gpt4 book ai didi

python - 从 Wunderground 解析 HTML 数据

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:32 25 4
gpt4 key购买 nike

全部,

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

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

由于我要下载全年的每小时数据,所以我需要编写脚本。但无论我尝试了什么(bash with 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com