Skip to content

Instantly share code, notes, and snippets.

@mekler
Created April 9, 2014 04:06
Show Gist options
  • Save mekler/10225176 to your computer and use it in GitHub Desktop.
Save mekler/10225176 to your computer and use it in GitHub Desktop.
Descarga datos de la página de la NOAA sección forecast
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)
@defvol
Copy link

defvol commented Apr 9, 2014

qué opinas de estandarizar el lenguaje?

p.ej.

[
    {
        "pronosticoValido": "23/0600Z",
        "latitudNorte": 16.3,
        "longitudOeste": 102.3,
        "vientos": {
            "sostenidos": 80,
            "rachas": 100
        }
    }
]

o

[
    {
        "forecastValid": "23/0600Z",
        "location": {
            "north": 16.3,
            "west": 102.3
        },
        "wind": {
            "max": 80,
            "gusts": 100
        }
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment