def gAPI(self, locat, region, main_location, country, link, f, g):
time.sleep(0.2);
geolocator = GoogleV3(api_key = 'AIzaSyDRb6HaVtHDbpHkJq8a3MEODFZlmkBt7f4')
if country != 'USA':
try:
searchTerm = str(locat + region + ',' + country)
location = geolocator.geocode(searchTerm)
except:
searchTerm = str(main_location + ',' + country)
location = geolocator.geocode(searchTerm)
extractCity = location.raw['address_components'] #Get the raw JSON information so that the city name can be extracted
city = locat
for item in extractCity:
types = item['types']
if types[0] == "locality":
city = item['long_name']
locat = country+'#'+str(city).decode("utf-8")+'#'+link+'#'+str(location.latitude)+'#'+str(location.longitude)
f.write(locat.encode('utf-8').replace(" ","").replace("\n",'')+'\n')
else:
try:
searchTerm = str(locat + region + ',' + main_location)
location = geolocator.geocode(searchTerm)
except:
raise Exception
extractCityState = location.raw['address_components'] #Get the raw JSON information so that the city name can be extracted
city = locat
state = ""
for item in extractCityState:
types = item['types']
if types[0] == "locality":
city = item['long_name']
elif types[0] == "administrative_area_level_1":
state = item['short_name']
if state == "":
raise Exception('No State!')
locat = 'USA#'+str(state)+'#'+str(city).decode("utf-8")+'#'+link+'#'+str(location.latitude)+'#'+str(location.longitude)
g.write(locat.encode('utf-8').replace(" ","").replace("\n",'')+'\n')
评论列表
文章目录