lolamontes69

zillow.py for Ch7 Programming Collective Intelligence

Jul 24th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. """ Helped from here: https://github.com/nico/collectiveintelligence-book """
  2.  
  3. import xml.dom.minidom
  4. import urllib2 as urllib2
  5.  
  6. zwskey = "YOUR_APIKEY_HERE"
  7.  
  8. def getaddressdata(address, city):
  9.   escad = address.replace(' ', '+')
  10.  
  11.   url = 'http://www.zillow.com/webservice/GetDeepSearchResults.htm?'
  12.   url += 'zws-id=%s&address=%s&citystatezip=%s' % (zwskey, escad, city)
  13.  
  14.   doc = xml.dom.minidom.parseString(urllib2.urlopen(url).read())
  15.   code = doc.getElementsByTagName('code')[0].firstChild.data
  16.   if code != '0': return None
  17.  
  18.   try:
  19.       zipcode = doc.getElementsByTagName('zipcode')[0].firstChild.data
  20.       use = doc.getElementsByTagName('useCode')[0].firstChild.data
  21.       year = doc.getElementsByTagName('yearBuilt')[0].firstChild.data
  22.       bath = doc.getElementsByTagName('bathrooms')[0].firstChild.data
  23.       bed = doc.getElementsByTagName('bedrooms')[0].firstChild.data
  24.       #rooms = doc.getElementsByTagName('totalRooms')[0].firstChild.data
  25.       price = doc.getElementsByTagName('amount')[0].firstChild.data
  26.   except:
  27.       return None
  28.  
  29.   return zipcode, use, int(year), float(bath), int(bed), price
  30.  
  31. def getpricelist():
  32.     l1=[]
  33.     for line in file('addresslist.txt'):
  34.         data=getaddressdata(line.strip(),'Irvington,NJ')
  35.         if data==None: pass
  36.         else: l1.append(data)
  37.         print data
  38.     return l1
  39.  
  40. if __name__ == '__main__':
  41.     import treepredict as treepredict
  42.     housedata = getpricelist()
  43.     tree = treepredict.buildtree(housedata, scoref=treepredict.variance)
  44.     treepredict.drawtree(tree, 'housetree.jpg')
  45.     print "Created housetree.jpg"
  46.  
  47. """
  48. example api call http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=YOUR_APIKEY_HERE&address=22+Franklin+Ter&citystatezip=Irvington+NJ
  49. """
Advertisement
Add Comment
Please, Sign In to add comment