| | |
Searching Weather for a term with urllib2 module
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
This is what I've learnt on searching for a term in a site[Google, here]
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:
python Syntax (Toggle Plain Text)
import urllib2 as url import os import time os.system('cls') print("[Content provided by The Weather Channel]") time.sleep(3) os.system('cls') print("Please wait...this may take a few seconds.") time.sleep(3) os.system('cls') condition=True try: url_open=url.urlopen("http://www.weather.com") lines=url_open.readlines()[1192:1199] except: condition=False if(condition): print("Weather forecast for Bangalore, INDIA\n") for x in lines: onsplit=x.split(">") tags=onsplit[2][:-5] if(tags=="Pressure:"): data=onsplit[4][:5] elif(tags=="Dew Point:"): data=onsplit[4][:2] else: data=onsplit[4][:-5] print(tags+" "+data) raw_input("\n<Any key to quit>")
This is what I've learnt on searching for a term in a site[Google, here]
python Syntax (Toggle Plain Text)
import urllib2 as url2 import urllib as url values={"search":"city"} data=url.urlencode(values) req=url2.Request("http://www.google.com",data) res=url2.urlopen(req) print(res.read())
•
•
Join Date: Jul 2009
Posts: 31
Reputation:
Solved Threads: 2
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.
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.
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?
•
•
Join Date: Jul 2009
Posts: 31
Reputation:
Solved Threads: 2
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/
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/
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 ...
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 ...
python Syntax (Toggle Plain Text)
# 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) """
May 'the Google' be with you!
•
•
Join Date: Feb 2008
Posts: 37
Reputation:
Solved Threads: 5
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
--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)
"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)
![]() |
Similar Threads
- How do I make this kind of an Internet app? (Python)
- Database normalization/redundancy question (PHP)
- Can I grab info from webpage? (Python)
- how to? (Python)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Read part of text file (PHP)
- Help (Perl)
- brain stroming ideas for program (Python)
- When a host says "included scripts" (Networking Hardware Configuration)
- I need window~1.ani (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: pygame and opengl
- Next Thread: complicated file parsing
| Thread Tools | Search this Thread |
adsense advertising ajax analytics android aol api apple array beginner bing blackberry blogging browser business c++ cellphone chrome cloudcomputing code coding copyright data development doubleclick earnings economy email engine europe facebook file g1 gdata gmail google googledocs googleearth government hardware hp ibm internet iphone java linux malware maps marketing media microsoft mobile msn mysql netbooks networking news office onlineads opensource os pagerank php privacy programming publishing python revenue rss saas search searchengine security seo sex smartphones socialnetwork socialnetworking software statistics storage survey t-mobile technologystocks twitter ubuntu uk verizon video wave web website wiki wikipedia windows wolframalpha wxpython yahoo yahoo! youtube







