开发者

Parsing METAR webpage in python

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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜