Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing METAR webpage in python

Tags:

python

parsing

I need to use METAR weather information in a python script. I found http://pypi.python.org/pypi/metar/1.4.0 which seems like it should work for what I need for current METARs. However, I also need to use archived weather information.

I found Navlost.eu, which seems to work well for what I need. For example, http://www.navlost.eu/aero/metar/?icao=KBOS&dt0=2010-07-14+02%3A00%3A00&c=1&rt=metar

The python METAR module accesses a text file and parses that. How do I parse this webpage in a similar manner so that I am only grabbing the "KBOS 140154Z 15006KT 8SM -RA OVC034 23/22 A2994" text in this example?

like image 985
Thursdays Coming Avatar asked Mar 31 '26 21:03

Thursdays Coming


1 Answers

Looking at the raw HTML returned by the above link, you can see the METAR data nested between <code> tags:

<p><hr/><br/><code>KBOS 140154Z 15006KT 8SM -RA OVC034 23/22 A2994</code><br/><br/>

So use a Python regular expression to get at it:

import urllib2
import re

URL="http://www.navlost.eu/aero/metar/?icao=KBOS&dt0=2010-07-14+02%3A00%3A00&c=1&rt=metar"
f = urllib2.urlopen(URL)
data = f.read()

r = re.compile('<code>(.*)</code>', re.I | re.S | re.M)
print r.findall(data)[0]

The regular expression is found on the re.compile line, and the (.*) means that you're interested in all characters between the parentheses. The function r.findall returns all strings that match the expression, and [0] just gives the first one.

The following is the output:

KBOS 140154Z 15006KT 8SM -RA OVC034 23/22 A2994
like image 178
Darren Griffith Avatar answered Apr 03 '26 09:04

Darren Griffith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!