Python: getting location through IP address© getcodify.com

Python: getting location through IP address

Python: getting location through IP address

Target Website: https://my.ip.cn/

import requests
import time

url = "https://my.ip.cn/api/index?ip=&type=0"

headers = {
":authority": "my.ip.cn",
":method": "GET",
":path": "/api/index?ip=&type=0",
":scheme": "https",
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-CN,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,en-US;q=0.6,zh-TW;q=0.5,ja;q=0.4",
"cookie":"__cfduid=dfd9a8ad770a7d54ed934a6adb02390651607854807; INIT_IP_INFO=%E4%B8%AD%E5%9B%BD++%E9%BB%91%E9%BE%99%E6%B1%9F%E7%9C%81+%E5%93%88%E5%B0%94%E6%BB%A8%E5%B8%82+%E8%81%94%E9%80%9A",
"referer": "https://my.ip.cn/",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
"x-requested-with": "XMLHttpRequest"
}
Result = requests.get(url).json()
## IP
print(Result['ip'])
## Address
print(Result['address'])
City = Result['address'].split(" ")[-2]

Python: getting location through IP address

https://karobben.github.io/2020/12/13/Blog/python_loc_get/

Author

Karobben

Posted on

2020-12-13

Updated on

2024-01-11

Licensed under

Comments