1,105,450 Community Members

Python Data Mashup - Geocode

Member Avatar
donkatsu
Newbie Poster
2 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I'm new to Python and am having trouble with this.
Here's my code:

import urllib, urllib2, re
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup

# The get_zipcode(address) function will be called with an address string with
# no zip code, such as 'Lowell Observatory, Flagstaff, AZ', and it should return the
# ZIP code of the address as a string
# For example, get_zipcode('Lowell Observatory, Flagstaff, AZ') should return '86001'
# Use the Google Geocoding API to obtain the ZIP code. Documentation:
# https://developers.google.com/maps/documentation/geocoding/
# Can use either json or xml output format.
def get_zipcode(address):
  zipcode = ''
  url_values = urllib.urlencode(address)
  url="http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % address
  #return zipcode
  response = urllib2.urlopen(url)
  return response

I'm getting an error and don't really know how to continue to get the output I want. Especially with extracting the zip code from the json output. Any help would be greatly apprecited!

Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

So, what is your error message?

Member Avatar
ZZucker
Practically a Posting Shark
829 posts since Jan 2008
Reputation Points: 313 [?]
Q&As Helped to Solve: 89 [?]
Skill Endorsements: 2 [?]
 
0
 

Convert the json string object to a Python dictionary and look at the dictionary keys.

Member Avatar
donkatsu
Newbie Poster
2 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Actually, I solved the zipcode part. I ended up using the xml output to grab the data. :D
But now I really have no idea how to get the current weather conditions from the rss weather feed.
Right now, this is my code:

def get_weather(zipcode):
  weather = ''
  # +++your code here+++
  url1 = "http://rss.weather.com/weather/rss/local/"
  url2 = url1 + zipcode
  response = urllib2.urlopen(url2).read()
  root = ET.fromstring(response)
  print root

I have to go to the item tag that has the Current weather conditions (from http://rss.weather.com/weather/rss/local/92802) and then grab the link under that tag, but I don't know how to do either especially since it has this weird <![CDATA[ in front.

<link>
<![CDATA[
http://www.weather.com/weather/local/92802?cm_pla=city_page&cm_ite=cc&site=city_page&cm_ven=LWO&cm_cat=&par=LWO_
]]>
</link>

Also, since I'm doing this for multiple zipcodes and cities, I don't know how to set it so it reads only that item tag...
Hopefully that makes sense.

Question Answered as of 1 Year Ago by Lardmeister and ZZucker
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article