Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ geoGrab() and massPlaceFind() from Machine Learning in Action Chapter 10.
- Seems as though the book versions were not working I wrote versions
- using YQL that parses the returned XML for the latitudes and longitudes.
- After all I wouldn't want to miss out on Drew's Pubcrawl :)
- --lolamontes69
- """
- import urllib2 as urllib2
- import xml.etree.ElementTree as ET
- from time import sleep
- def geoGrab(address):
- apiStem = 'http://query.yahooapis.com/v1/public/yql?q='
- apiStem1 = 'select%20*%20from%20geo.placefinder%20where%20text="'
- apiTail = '"'
- address = address.replace(' ','%20')
- url = apiStem+apiStem1+address+apiTail
- print url
- geoxml = urllib2.urlopen(url)
- tree = ET.parse(geoxml)
- root = tree.getroot()
- latitude = (root[0][0].find('latitude')).text
- longitude = (root[0][0].find('longitude')).text
- print float(latitude),float(longitude)
- return float(latitude),float(longitude)
- def massPlaceFind(fileName):
- fw = open('places.txt', 'w')
- for line in open(fileName).readlines():
- line = line.strip()
- line1 = line.split('\t')
- lineArr = ' '.join(line1[1:])
- try: latt,lng = geoGrab(lineArr)
- except: pass
- try:
- print "%s\t%f\t%f" % (lineArr, latt, lng)
- fw.write('%s\t%f\t%f\n' % (line, latt, lng))
- except: print "error fetching"
- sleep(1)
- fw.close()
- if __name__ == "__main__":
- massPlaceFind("portlandClubs.txt")
Advertisement
Add Comment
Please, Sign In to add comment