Following the thinking of sneekula at:
http://www.daniweb.com/forums/post892908.html#post892908
You can do some detective work and pull weather data from the www.weather.com html code like my little example shows ...
# given the zip code, extract the weather conditions of the area
# tested with Python25
import urllib2
import time
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)[0]
zipcode = '91201'
url_str = 'http://www.weather.com/weather/local/' + zipcode
try:
fin = urllib2.urlopen(url_str)
html = fin.readlines()
fin.close()
except IOError:
print( 'Cannot open URL %s for reading' % url_str )
html = False
if html:
for line in html:
#print( line ) # test
if line.startswith('OAS_spoof'):
location = line
if line.startswith('OAS_query'):
weather = line
#print( location ) # test
#print( weather ) # test
location_list = location.split('/')
#print( location_list ) # test
town = location_list[9].capitalize()
state = location_list[7].capitalize()
zip = location_list[10][: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)
"""
vegaseat
DaniWeb's Hypocrite
6,499 posts since Oct 2004
Reputation Points: 1,451
Solved Threads: 1,618
Skill Endorsements: 37
Question Answered as of 3 Years Ago by
foosion,
EAnder
and
vegaseat