I am making a program called 'Weather Watch' which basically gets weather updates for any city you type in.
For now, it only gets info for a particular city. I don't know how to search for the term entered in www.weather.com and then get the updates. The code so far:
import urllib2 as url
print("[Content provided by The Weather Channel]")
print("Please wait...this may take a few seconds.")
print("Weather forecast for Bangalore, INDIA\n")
for x in lines:
raw_input("\n<Any key to quit>")
This is what I've learnt on searching for a term in a site[Google, here]
import urllib2 as url2
import urllib as url
Both weather.com and google have APIs for accessing weather info. Basically, you form a URL in a specified format and get and XML page with current weather and forecasts. Weather.com requires signing up in advance, at no cost.
foosion, I did not understand you fully. On what format is the URL formed? If I get the URL right for all cities, I can take care of 'getting' the data, by slicing and so on. So, how is the URL formed?
# given the zip code, extract the weather conditions of the area
# tested with Python25
def extract(text, sub1, sub2):
extract a substring from text between first
occurances of substrings sub1 and sub2
return text.split(sub1, 1)[-1].split(sub2, 1)
zipcode = '91201'
url_str = 'http://www.weather.com/weather/local/' + zipcode
fin = urllib2.urlopen(url_str)
html = fin.readlines()
print( 'Cannot open URL %s for reading' % url_str )
html = False
for line in html:
#print( line ) # test
location = line
weather = line
#print( location ) # test
#print( weather ) # test
location_list = location.split('/')
#print( location_list ) # test
town = location_list.capitalize()
state = location_list.capitalize()
zip = location_list[:5]
print( time.strftime("%A, %d%b%Y at %H:%M hours", time.localtime()) )
print( "Lovely %s, %s %s" % (town, state, zip) )
temp_now = extract(weather, 'temp=', '&')
cond_now = extract(weather, 'cond=', '&')
temp_high = extract(weather, 'temph1=', '&')
temp_low = extract(weather, 'templ1=', '&')
sf = "is %s with %sF (low=%sF and high=%sF)"
print( sf % (cond_now, temp_now, temp_low, temp_high) )
"""my result -->
Tuesday, 08Sep2009 at 13:15 hours
Lovely Glendale, Ca 91201
is clear_sunny with 81F (low=62F and high=84F)
I did something very similar for my father who happens to be a weather nut. I went online and found the webpage for our zipcode and used urllib to get updates of the webpage every 15 minutes or so. Then I just parsed the html for common things like temperature, dew point, cloud coverage. It was pretty simple once you knew what to look for. My solution worked fine for me but if you plan on sharing this program with more than you or maybe a family member I would recomend using the APIs as they will probably be more reliable. If the weather channel were to change their webpage design my little program could be useless.