IP 주소를 위도와 경도로 변환

Web Scraping with Python을 읽다가 IP 주소를 위도와 경도로 알려주는 사이트를 봤다.
csv, xml, json의 형태로 데이터를 반환하기 때문에
수업 시간에 다양하게 활용할 수 있을 것 같아 정리해 둔다.

import requests
import json

# 네이버 202.131.30.11
# 구글 173.194.126.240
# 용인 수지 121.165.11.161

# 내 컴퓨터 IP 주소
recvd = requests.get('http://ip.jsontest.com/')
my_ip = json.loads(recvd.text)
print(my_ip['ip'])

# ----------------------------------------------------------------- #

# ip가 가리키는 geo 정보 획득

# host 이름과 ip 주소 모두 사용 가능
hosts = ['www.naver.com', 'github.com', '202.131.30.11', '173.194.126.240', '121.165.11.161']

for host in hosts:
recvd = requests.get('http://www.freegeoip.net/xml/' + host)
geo =recvd.text
print(geo)

# <Response>
# <IP>23.193.193.7</IP>
# <CountryCode>US</CountryCode>
# <CountryName>United States</CountryName>
# <RegionCode>MA</RegionCode>
# <RegionName>Massachusetts</RegionName>
# <City>Cambridge</City>
# <ZipCode>02142</ZipCode>
# <TimeZone>America/New_York</TimeZone>
# <Latitude>42.3626</Latitude>
# <Longitude>-71.0843</Longitude>
# <MetroCode>506</MetroCode>
# </Response>
#
# <Response>
# <IP>192.30.253.112</IP>
# <CountryCode>US</CountryCode>
# <CountryName>United States</CountryName>
# <RegionCode>CA</RegionCode>
# <RegionName>California</RegionName>
# <City>San Francisco</City>
# <ZipCode>94107</ZipCode>
# <TimeZone>America/Los_Angeles</TimeZone>
# <Latitude>37.7697</Latitude>
# <Longitude>-122.3933</Longitude>
# <MetroCode>807</MetroCode>
# </Response>
#
# <Response>
# <IP>202.131.30.11</IP>
# <CountryCode>KR</CountryCode>
# <CountryName>Republic of Korea</CountryName>
# <RegionCode></RegionCode>
# <RegionName></RegionName>
# <City></City>
# <ZipCode></ZipCode>
# <TimeZone>Asia/Seoul</TimeZone>
# <Latitude>37.5112</Latitude>
# <Longitude>126.9741</Longitude>
# <MetroCode>0</MetroCode>
# </Response>
#
# <Response>
# <IP>173.194.126.240</IP>
# <CountryCode>US</CountryCode>
# <CountryName>United States</CountryName>
# <RegionCode>CA</RegionCode>
# <RegionName>California</RegionName>
# <City>Mountain View</City>
# <ZipCode>94043</ZipCode>
# <TimeZone>America/Los_Angeles</TimeZone>
# <Latitude>37.4192</Latitude>
# <Longitude>-122.0574</Longitude>
# <MetroCode>807</MetroCode>
# </Response>
#
# <Response>
# <IP>121.165.11.161</IP>
# <CountryCode>KR</CountryCode>
# <CountryName>Republic of Korea</CountryName>
# <RegionCode>41</RegionCode>
# <RegionName>Gyeonggi-do</RegionName>
# <City>Suwon</City>
# <ZipCode></ZipCode>
# <TimeZone>Asia/Seoul</TimeZone>
# <Latitude>37.2911</Latitude>
# <Longitude>127.0089</Longitude>
# <MetroCode>0</MetroCode>
# </Response>


for host in hosts:
recvd = requests.get('http://www.freegeoip.net/json/' + host)
geo = json.loads(recvd.text)
print(geo)

# {'longitude': -71.0843, 'zip_code': '02142', 'time_zone': 'America/New_York', 'ip': '23.193.193.7', 'latitude': 42.3626, 'city': 'Cambridge', 'country_code': 'US', 'country_name': 'United States', 'metro_code': 506, 'region_code': 'MA', 'region_name': 'Massachusetts'}
# {'longitude': -122.3933, 'zip_code': '94107', 'time_zone': 'America/Los_Angeles', 'ip': '192.30.253.112', 'latitude': 37.7697, 'city': 'San Francisco', 'country_code': 'US', 'country_name': 'United States', 'metro_code': 807, 'region_code': 'CA', 'region_name': 'California'}
# {'longitude': 126.9741, 'zip_code': '', 'time_zone': 'Asia/Seoul', 'ip': '202.131.30.11', 'latitude': 37.5112, 'city': '', 'country_code': 'KR', 'country_name': 'Republic of Korea', 'metro_code': 0, 'region_code': '', 'region_name': ''}
# {'longitude': -122.0574, 'zip_code': '94043', 'time_zone': 'America/Los_Angeles', 'ip': '173.194.126.240', 'latitude': 37.4192, 'city': 'Mountain View', 'country_code': 'US', 'country_name': 'United States', 'metro_code': 807, 'region_code': 'CA', 'region_name': 'California'}
# {'longitude': 127.0089, 'zip_code': '', 'time_zone': 'Asia/Seoul', 'ip': '121.165.11.161', 'latitude': 37.2911, 'city': 'Suwon', 'country_code': 'KR', 'country_name': 'Republic of Korea', 'metro_code': 0, 'region_code': '41', 'region_name': 'Gyeonggi-do'}


for host in hosts:
recvd = requests.get('http://www.freegeoip.net/csv/' + host)
print(recvd.text)

# 23.193.193.7,US,United States,MA,Massachusetts,Cambridge,02142,America/New_York,42.3626,-71.0843,506
# 192.30.253.112,US,United States,CA,California,San Francisco,94107,America/Los_Angeles,37.7697,-122.3933,807
# 202.131.30.11,KR,Republic of Korea,,,,,Asia/Seoul,37.5112,126.9741,0
# 173.194.126.240,US,United States,CA,California,Mountain View,94043,America/Los_Angeles,37.4192,-122.0574,807
# 121.165.11.161,KR,Republic of Korea,41,Gyeonggi-do,Suwon,,Asia/Seoul,37.2911,127.0089,0

'파이썬' 카테고리의 다른 글

import 정리  (0) 2017.05.01
현재 파일에 정의된 main 함수 호출  (0) 2017.04.19
줄 바꿈 없이 계속 출력  (0) 2017.04.19
numba - 성능 업!  (0) 2017.04.18
pprint : 쉽고 예쁘게 출력하기  (0) 2017.04.09