title: Obtain text file with temperature values
body: <p>By the end of it, I am supposed to have a .txt file with a temperature value for every day of 2009. The problem is, the file this code creates is only giving me 12 values (one for each month) and half of them are from dates that aren't real (i.e. April 31).</p>
  
  <p>I'm not fluent in Python, but I've reviewed my textbook and the code several times and can find no discrepancies.</p>
  
  <pre><code>import urllib2
  from bs4 import BeautifulSoup
  
  #CSV
  f = open('wunder-data.txt', 'w')
  
  #months, days
  for m in range(1, 13):
      for d in range(1, 32):
  
       #get if already gone through month
       if (m == 2 and d &gt; 28):
         break
       elif (m in [4, 6, 9, 11] and d &gt; 30):
         break
  
       #open wunderground.com url
       timestamp = '2009' + str(m) + str(d)
       print "Getting data for " + timestamp
       url = "http://www.wunderground.com/history/airport/KBUF/2009/" + str(m) + "/" + str(d) + "/DailyHistory.html"
       page = urllib2.urlopen(url)
  
      #get temp from page
      soup = BeautifulSoup(page)
      #dayTemp = soup.body.nobr.b.string
      dayTemp = soup.findAll(attrs={"class":"nobr"})[4].span.string
  
      #Format month for timestamp
      if len(str(m)) &lt; 2:
       mStamp = '0' + str(m)
      else:
       mStamp = str(m)
  
      #Format day for timestamp
      if len(str(d)) &lt; 2:
       dStamp = '0' + str(d)
      else:
       dStamp = str(d)
  
      #Build timestamp
      timestamp = '2009' + mStamp + dStamp
  
      #Write timestamp and temperature to file
      f.write(timestamp + ',' + dayTemp + '\n')
  
  # Done getting data! Close file.
  f.close()
  </code></pre>