1. 程式人生 > >資料視覺化指南之資料獲取

資料視覺化指南之資料獲取

#-*- encoding:utf-8 -*-
import urllib2                          #1
from bs4 import BeautifulSoup            #2
f = open('temp-data.txt','w')             #3
for m in range(1,13):                      #4                                                                                                                    
    for d in range(1,32):
        if (m==2 and d>28):                                                                                                                                                                           #5 
            break
        elif(m in [4,6,9,11] and d>30):                                                                                                                                                               #6
            break
        timestamp = '2009' + str(m) + str(d)    #7
        print "Geting Data for:"+timestamp
        url = "http://www.wunderground.com/history/airport/KBUF/2009/" + str(m) + "/" + str(d)+"/DailyHistory.html"     #8
        page = urllib2.urlopen(url)                                        #9
        soup = BeautifulSoup(page)                                         #10
        dayTemp = soup.findAll( attrs = {"class":"wx-value"})[4].string    #11
        
        if len(str(m)) < 2:                                                                                                                                                                                                           #12
            mStamp = '0'+str(m)
        else:
            mStamp = str(m)
        if len(str(d)) < 2:
            dStamp = '0'+ str(d)
        else:
            dStamp = str(d)
        timestamp = '2009' + mStamp + dStamp                                  #13
        f.write(timestamp + ':'+dayTemp + '\n')                               #14
f.close()                                                                     #15