Searching Weather for a term with urllib2 module

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Searching Weather for a term with urllib2 module

 
0
  #1
Sep 8th, 2009
Hey guys,

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:
  1. import urllib2 as url
  2. import os
  3. import time
  4.  
  5. os.system('cls')
  6.  
  7. print("[Content provided by The Weather Channel]")
  8. time.sleep(3)
  9. os.system('cls')
  10.  
  11. print("Please wait...this may take a few seconds.")
  12. time.sleep(3)
  13. os.system('cls')
  14.  
  15. condition=True
  16.  
  17. try:
  18. url_open=url.urlopen("http://www.weather.com")
  19. lines=url_open.readlines()[1192:1199]
  20. except:
  21. condition=False
  22.  
  23. if(condition):
  24. print("Weather forecast for Bangalore, INDIA\n")
  25. for x in lines:
  26. onsplit=x.split(">")
  27.  
  28. tags=onsplit[2][:-5]
  29.  
  30. if(tags=="Pressure:"):
  31. data=onsplit[4][:5]
  32. elif(tags=="Dew Point:"):
  33. data=onsplit[4][:2]
  34. else:
  35. data=onsplit[4][:-5]
  36.  
  37. print(tags+" "+data)
  38. raw_input("\n<Any key to quit>")

This is what I've learnt on searching for a term in a site[Google, here]

  1. import urllib2 as url2
  2. import urllib as url
  3.  
  4. values={"search":"city"}
  5.  
  6. data=url.urlencode(values)
  7.  
  8. req=url2.Request("http://www.google.com",data)
  9. res=url2.urlopen(req)
  10.  
  11. print(res.read())
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 31
Reputation: foosion is an unknown quantity at this point 
Solved Threads: 2
foosion foosion is offline Offline
Light Poster

Re: Searching Weather for a term with urllib2 module

 
0
  #2
Sep 8th, 2009
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.

http://www.weather.com/services/xmloap.html
A google example:
http://www.google.com/ig/api?weather=New%20York

There are a bunch of python examples out there. You have to parse XML, but it's pretty easy.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: Searching Weather for a term with urllib2 module

 
0
  #3
Sep 8th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 31
Reputation: foosion is an unknown quantity at this point 
Solved Threads: 2
foosion foosion is offline Offline
Light Poster

Re: Searching Weather for a term with urllib2 module

 
0
  #4
Sep 8th, 2009
It depends on which service you use and what info you want.

Here's an example for google (and yahoo and noaa) weather. It was the first hit when I used google to search for "python weather google." http://code.google.com/p/python-weather-api/
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,030
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Searching Weather for a term with urllib2 module

 
0
  #5
Sep 8th, 2009
Following the thinking of sneekula at:
http://www.daniweb.com/forums/post89...tml#post892908
You can do some detective work and pull weather data from the www.weather.com html code like my little example shows ...
  1. # given the zip code, extract the weather conditions of the area
  2. # tested with Python25
  3.  
  4. import urllib2
  5. import time
  6.  
  7. def extract(text, sub1, sub2):
  8. """
  9. extract a substring from text between first
  10. occurances of substrings sub1 and sub2
  11. """
  12. return text.split(sub1, 1)[-1].split(sub2, 1)[0]
  13.  
  14.  
  15. zipcode = '91201'
  16. url_str = 'http://www.weather.com/weather/local/' + zipcode
  17. try:
  18. fin = urllib2.urlopen(url_str)
  19. html = fin.readlines()
  20. fin.close()
  21. except IOError:
  22. print( 'Cannot open URL %s for reading' % url_str )
  23. html = False
  24.  
  25. if html:
  26. for line in html:
  27. #print( line ) # test
  28. if line.startswith('OAS_spoof'):
  29. location = line
  30. if line.startswith('OAS_query'):
  31. weather = line
  32.  
  33. #print( location ) # test
  34. #print( weather ) # test
  35.  
  36. location_list = location.split('/')
  37. #print( location_list ) # test
  38.  
  39. town = location_list[9].capitalize()
  40. state = location_list[7].capitalize()
  41. zip = location_list[10][:5]
  42.  
  43. print( time.strftime("%A, %d%b%Y at %H:%M hours", time.localtime()) )
  44. print( "Lovely %s, %s %s" % (town, state, zip) )
  45.  
  46. temp_now = extract(weather, 'temp=', '&')
  47. cond_now = extract(weather, 'cond=', '&')
  48. temp_high = extract(weather, 'temph1=', '&')
  49. temp_low = extract(weather, 'templ1=', '&')
  50.  
  51. sf = "is %s with %sF (low=%sF and high=%sF)"
  52. print( sf % (cond_now, temp_now, temp_low, temp_high) )
  53.  
  54. """my result -->
  55. Tuesday, 08Sep2009 at 13:15 hours
  56. Lovely Glendale, Ca 91201
  57. is clear_sunny with 81F (low=62F and high=84F)
  58. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 37
Reputation: EAnder is an unknown quantity at this point 
Solved Threads: 5
EAnder EAnder is offline Offline
Light Poster

Re: Searching Weather for a term with urllib2 module

 
1
  #6
Sep 10th, 2009
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.

--EAnder
"There are only two industries that refer to their customers as 'users'." (Edward Tufte)
"There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."(C.A.R. Hoare)
Reply With Quote Quick reply to this message  
Reply

Tags
data, google, module, python, urllib, urllib2

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC