Created
April 9, 2014 04:06
-
-
Save mekler/10225176 to your computer and use it in GitHub Desktop.
Descarga datos de la página de la NOAA sección forecast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
import re | |
try: | |
from urllib2 import urlopen | |
except ImportError: | |
from urllib.request import urlopen # py3k | |
def scrapeNOAA(url): | |
soup = BeautifulSoup(urlopen(url)) | |
texto = soup.pre.text | |
renglones = texto.split("\n") | |
n = len(renglones) | |
vientos = len('MAX WIND') | |
rachas = len('GUSTS') | |
respuesta = [] | |
for i in range(0,n): | |
#FORECAST VALID 15/0600Z 17.9N 102.7W | |
#[ { forecastValid: "23/0600Z", latNorth: "16.3", lngWest: "102.3", vientos: { sostenidos: 80, rachas: 100 } | |
m = re.search(r'^FORECAST VALID [0-9]+/[0-9A-Z]+ .*W(?=.*)',renglones[i]) | |
if m is not None: | |
forecast = {} | |
cabecera = m.group() | |
cabecera = re.sub(' +',' ',cabecera) | |
aux = cabecera.split(' ') | |
if(len(aux)==5): | |
forecast['forecastValid'] = aux[2] | |
forecast['latNorth'] = aux[3] | |
forecast['lngWest'] = aux[4] | |
i = i+1 | |
renglones[i] = re.sub(' +',' ',renglones[i]) | |
forecast['vientos']= { 'sostenidos':renglones[i][renglones[i].find('MAX WIND')+vientos:renglones[i].find('KT')].strip(), 'rachas':renglones[i][renglones[i].find('GUSTS')+rachas:-3].strip() } | |
i = i+1 | |
j = 0 | |
while len(renglones[i].strip())>0 and i<n: | |
dato = re.sub(' +',' ',renglones[i]).split(' ') | |
forecast['dato'+str(j)] = {'KT':dato[0],'NE':dato[2][:-2],'SE':dato[3][:-2],'SW':dato[4][:-2],'NW':dato[5][:-3]} | |
j = j+1 | |
i = i+1 | |
respuesta.append(forecast) | |
return respuesta | |
url = 'http://www.nhc.noaa.gov/archive/2013/ep13/ep132013.fstadv.002.shtml?text' | |
print scrapeNOAA(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
qué opinas de estandarizar el lenguaje?
p.ej.
o