IP address lookup of country name, city name, latitude and longitude

by

 

Finds the country name, region name, city name, latitude and longitude from an IP address.

IP_location_lookup(ip_address_as_string)
function [country_name, region_name, city_name,latitude_string, longitude_string] = IP_location_lookup(ip_address_as_string)
%% input an IP address as a string such as '128.95.155.134'
% returns the country name, the city name

ip_url = ['http://www.geobytes.com/IpLocator.htm?GetLocation&IpAddress='  ip_address_as_string];
str = urlread(ip_url);          % get html available at url requested

% get country
start = strfind(str,'<input name="ro-no_bots_pls13" value="') + 38;     % within the html look for the country name field - this can be edited for other fields
grab = str(start:(start+50));   % get a chunk of text beginning with the country name
name_end = strfind(grab,'"')-1; % find the " symbol that ends the country name
country_name = grab(1:name_end(1));      % select just the name of the country

% get city
start = strfind(str,'<input name="ro-no_bots_pls17" value="') + 38;     % within the html look for the country name field - this can be edited for other fields
grab = str(start:(start+50));   % get a chunk of text beginning with the country name
name_end = strfind(grab,'"')-1; % find the " symbol that ends the country name
city_name = grab(1:name_end(1));      % select just the name of the city

% get region
start = strfind(str,'<input name="ro-no_bots_pls15" value="') + 38;     % within the html look for the country name field - this can be edited for other fields
grab = str(start:(start+50));   % get a chunk of text beginning with the country name
name_end = strfind(grab,'"')-1; % find the " symbol that ends the country name
region_name = grab(1:name_end(1));      % select just the name of the region

% get latitude
start = strfind(str,'<input name="ro-no_bots_pls10" value="') + 38;     % within the html look for the country name field - this can be edited for other fields
grab = str(start:(start+50));   % get a chunk of text beginning with the country name
name_end = strfind(grab,'"')-1; % find the " symbol that ends the country name
latitude_string = grab(1:name_end(1));      % select just the name of the region

% get longitude
start = strfind(str,'<input name="ro-no_bots_pls19" value="') + 38;     % within the html look for the longitude field - this can be edited for other fields
grab = str(start:(start+50));   % get a chunk of text beginning with the longitude name
name_end = strfind(grab,'"')-1; % find the " symbol that ends the longitude name
longitude_string = grab(1:name_end(1));      % select just the longitude
end

Contact us