1

我在 Python 方面仍然非常陌生,但我正在尝试编写代码来解析来自 NOAA 的天气并按照我们的无线电广播的顺序显示它。

我已经设法整理了一个使用 python 表达式的当前条件列表,其中 html 文件被分割成行列表,然后以正确的顺序重新输出,但每一个都是单行数据. 该代码如下所示:

#other function downloads  
#http://www.arh.noaa.gov/wmofcst_pf.php?wmo=ASAK48PAFC&type=public
#and renames it currents.html
from bs4 import BeautifulSoup as bs
import re
soup = bs(open('currents.html')
weatherRaw = soup.pre.string
towns = ['PAOM', 'PAUN', 'PAGM', 'PASA']
townOut = []
weatherLines = weatherRaw.splitlines()
for i in range(len(towns)):
    p = re.compile(towns[i] + '.*')
    for line in weatherLines:
        matched = p.match(line)
        if matched:
            townOut.append(matched.group())

现在我正在处理预测部分,我遇到了一个问题,因为每个预测都必须跨越多行,并且我已经将文件切分为行列表。

所以:我正在寻找的是一个允许我使用类似循环的表达式,这次从找到的行开始追加,并在仅包含 && 的行结束。像这样的东西:

#sample data from http://www.arh.noaa.gov/wmofcst.php?wmo=FPAK52PAFG&type=public
#BeautifulSouped into list fcst (forecast.pre.get_text().splitlines())
zones = ['AKZ214', 'AKZ215', 'AKZ213'] #note the out-of-numerical-order zones
weatherFull = []
for i in range(len(zones)):
    start = re.compile(zones[i] '.*')
    end = re.compile('&&')
    for line in fcst:
        matched = start.match(line)
        if matched:
            weatherFull.append(matched.group())
            #and the other lines of various contents and length
            #until reaching the end match object

我应该怎么做才能改进这段代码?我知道这很冗长,但是当我开始的时候,我喜欢能够跟踪我在做什么。提前致谢!

4

1 回答 1

0

如果这不是您所追求的,请道歉(在这种情况下,很乐意调整)。您使用 BeautifulSoup 真是太棒了,但您实际上可以更进一步。查看 HTML,似乎每个块都以一个<a name=zone>结构开始,并以下一个<a name=zone>. 在这种情况下,您可以执行以下操作来为每个区域提取相应的 HTML:

from bs4 import BeautifulSoup

# I put the HTML in a file, but this will work with a URL as well
with open('weather.html', 'r') as f:
  fcst = f.read()

# Turn the html into a navigable soup object
soup = BeautifulSoup(fcst)

# Define your zones
zones = ['AKZ214', 'AKZ215', 'AKZ213']

weatherFull = []

# This is a more Pythonic loop structure - instead of looping over
# a range of len(zones), simply iterate over each element itself
for zone in zones:
  # Here we use BS's built-in 'find' function to find the 'a' element
  # with a name = the zone in question (as this is the pattern).
  zone_node = soup.find('a', {'name': zone})

  # This loop will continue to cycle through the elements after the 'a'
  # tag until it hits another 'a' (this is highly structure dependent :) )
  while True:
    weatherFull.append(zone_node)
    # Set the tag node = to the next node
    zone_node = zone_node.nextSibling
    # If the next node's tag name = 'a', break out and go to the next zone
    if getattr(zone_node, 'name', None)  == 'a':
      break

# Process weatherFull however you like
print weatherFull

希望这会有所帮助(或者至少在您想要的范围内!)。

于 2012-10-29T01:41:15.573 回答