【问题标题】:Having trouble using BeautifulSoup to parse WeatherUnderground使用 BeautifulSoup 解析 Weather Underground 时遇到问题
【发布时间】:2011-11-02 05:30:11
【问题描述】:

我正在尝试修改代码以从 wunderground 中提取信息。然而,我正在尝试改编的剧本是在 2008 年编写的,并且地下天气的格式发生了变化。我在使用 soup.body.nobr.b.string 时遇到问题。我想从给定站点提取每日降水数据。 http://www.wunderground.com/history/airport/KBUF/2011/5/2/DailyHistory.html 导入 urllib2 从 BeautifulSoup 导入 BeautifulSoup

# Create/open a file called wunder.txt (which will be a comma-delimited file)
f = open('wunder-data.txt', 'w')

# Iterate through year, month, and day
for y in range(1980, 2007):
  for m in range(1, 13):
    for d in range(1, 32):

      # Check if leap year
      if y%400 == 0:
        leap = True
      elif y%100 == 0:
        leap = False
      elif y%4 == 0:
        leap = True
      else:
        leap = False

      # Check if already gone through month
      if (m == 2 and leap and d > 29):
        continue
      elif (m == 2 and d > 28):
        continue
      elif (m in [4, 6, 9, 10] and d > 30):
        continue

      # Open wunderground.com url
      url = "http://www.wunderground.com/history/airport/KBUF/"+str(y)+ "/" + str(m) + "/" + str(d) + "/DailyHistory.html"
      page = urllib2.urlopen(url)

      # Get temperature from page
      soup = BeautifulSoup(page)
      dayTemp = soup.body.nobr.b.string

      # Format month for timestamp
      if len(str(m)) < 2:
        mStamp = '0' + str(m)
      else:
        mStamp = str(m)

      # Format day for timestamp
      if len(str(d)) < 2:
        dStamp = '0' + str(d)
      else:
        dStamp = str(d)

      # Build timestamp
      timestamp = str(y) + mStamp + dStamp

      # Write timestamp and temperature to file
      f.write(timestamp + ',' + dayTemp + '\n')

# Done getting data! Close file.
f.close()

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    不要乱解析 HTML,它可能会再次更改,恕不另行通知。 获取one of their CSV files(HTML页面底部有链接),用csv模块解析。

    【讨论】:

    • 哈,真是天才。很好的发现:P
    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2017-07-16
    • 2017-06-16
    • 2018-04-25
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多