Use Python to collect weather data and write to file

Nardrek picture Nardrek · Jul 16, 2013 · Viewed 16.5k times · Source

I would like your advice/help on this:

Create a python script that:

Add the data collected from the website into a new line of the CSV file.

Rules:

  • The script must be running on your computer automatically for 5 days.

Do you have some advice? :(

I appreciate your help on this.

I tried this :

import urllib2
import cookielib

cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))

setURL = 'http://www.wunderground.com/global/stations/54511.html?MR=1'
request = urllib2.Request(setURL)
response = opener.open(request)

url = "http://www.wunderground.com/global/stations/54511.html?MR=1"
request = urllib2.Request(url)
page = opener.open(request)

WeatherData = page.read()                            
print WeatherData

So it print all the data but I want to print only the :

Datetime (timestamp of data captured) - Current Condition - Temperature

  • like I said need advice.like :

    • what should I use to complete this task.

    • How can set the data collect for (days), I don't know..

I don't need the full answer and copy paste, I'm not fool...

I want to UNDERSTAND.

Answer

user2286078 picture user2286078 · Jul 16, 2013

Weather Underground have posted a Python sample to access their API. I think you'd be best off using their code. If you play around with the parsed_json variable in the example, you should be able to get what you want.

As for running the program after fixed intervals, check out this thread.