Graphics Programs Reference
In-Depth Information
# Write timestamp and temperature to file
f.write(timestamp + ',' + dayTemp + '\n')
Then use f.close() when you finish with all the months and days.
# Done getting data! Close file.
f.close()
The only thing left to do is run the code, which you do in your terminal with
the following:
$ python get-weather-data.py
It takes a little while to run, so be patient. In the process of running, your
computer is essentially loading 365 pages, one for each day of 2009. You
should have a file named wunder-data.txt in your working directory when
the script is done running. Open it up, and there's your data, as a comma-
separated file. The first column is for the timestamps, and the second col-
umn is temperatures. It should look similar to Figure 2-6.
FIGurE 2-6 One year's worth of scraped temperature data
GENErALIzING ThE ExAMPLE
Although you just scraped weather data from Weather Underground, you
can generalize the process for use with other data sources. Data scraping
typically involves three steps:
1. Identify the patterns.
2. Iterate.
3. Store the data.
Search WWH ::




Custom Search