Graphics Programs Reference
In-Depth Information
Remember that you changed the URL manually to get the weather data for
the date you want. The preceding code is for January 1, 2009. If you want
the page for January 2, 2009, simply change the date portion of the URL
to match that. To get the data for every day of 2009, load every month (1
through 12) and then load every day of each month. Here's the script in full
with comments. Save it to your get-weather-data.py file.
import urllib2
from BeautifulSoup import BeautifulSoup
# Create/open a file called wunder.txt (which will be a comma-delimited
file)
f = open('wunder-data.txt', 'w')
# Iterate through months and day
for m in range(1, 13):
for d in range(1, 32):
# Check if already gone through month
if (m == 2 and d > 28):
break
elif (m in [4, 6, 9, 11] and d > 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 temperature from page
soup = BeautifulSoup(page)
# dayTemp = soup.body.nobr.b.string
dayTemp = soup.findAll(attrs={“class”:”nobr”})[5].span.string
# Format month for timestamp
if len(str(m)) < 2:
mStamp = '0' + str(m)
else:
mStamp = str(m)
# Format day for timestamp
Search WWH ::




Custom Search