continue place_tag = tag.find('td', {'style' : "border-bottom:#DDCFE6 1px solid;"}).contents[0].strip() # No hyperlink address_tag = tag.find('br').next_sibling.strip() if not place_tag: place_tag = tag.find('a', {'style' : "font-size:100%"}).contents[0].strip() # Hyperlinked if (address_tag.find('Unavailable') != -1): parsed_unavailable_addresses = parsed_unavailable_addresses + 1 if (place_tag == 'Map'): continue latitude = '' longitude = '' response = urllib.request.urlopen(url_header + urllib.parse.quote((address_tag).encode('utf-8'))) json_data = json.loads(response.read()) coords = flatten_json(json_data) try: latitude = coords['features_0_geometry_coordinates_1'] longitude = coords['features_0_geometry_coordinates_0'] # print("found coords: " + str(coords['features_0_geometry_coordinates_1']) + ", " +str(coords['features_0_geometry_coordinates_0'])) except KeyError: counter = counter + 1 # print("no coords") # print(full_location + " has no available coordinates, maybe its an address on a highway?") total = total + 1 if total % 100 == 0: print(str(total) + " records processed") # print(("location: " + current_location_type)) # print(("place: " + place_tag))
# You can substitute a hyphen (-) for any structured URL parameter when there is no value. url_header = f'http://dev.virtualearth.net/REST/v1/Locations/?countryRegion=US&adminDistrict={adminDistrict}&locality={locality}&postalCode={postalCode}&addressLine={addressLine}&maxResults={maxResults}&key={BingMapsAPIKey}' # url_header_encoded = urllib.parse.quote(url_header) # print(url_header) response = requests.get(url_header) count += 1 if response.status_code == 200: successful += 1 coords = flatten_json(response.json()) latitude = coords['resourceSets_0_resources_0_point_coordinates_0'] longitude = coords['resourceSets_0_resources_0_point_coordinates_1'] # print(str(latitude) + ", " + str(longitude)) source_csv.loc[index, 'latitude'] = latitude source_csv.loc[index, 'longitude'] = longitude source_csv.loc[index, 'api_failed_to_resolve'] = "N" source_csv.loc[index, 'geolocation_source'] = "bing" else: missing += 1 source_csv.loc[index, 'api_failed_to_resolve'] = "Y" source_csv['latlong_found_after'] = source_csv['latitude'] source_csv['latlong_found_after'] = numpy.where(pandas.isna(source_csv.latitude),'N', 'Y') print(tabulate(source_csv.groupby(['latlong_found', 'latlong_found_after', 'removed']).size().reset_index().rename(columns={0:'count'}), headers='keys', tablefmt='psql', showindex=False))