Home  >  Article  >  Can the specific address be found based on the IP address?

Can the specific address be found based on the IP address?

青灯夜游
青灯夜游Original
2020-11-06 12:06:31117412browse

The specific address can be found based on the IP address; the IP address is an "ID card" obtained based on the network connection point of each computer. Querying the IP address allows us to effectively know where the computer is. .

Can the specific address be found based on the IP address?

The IP address is an "ID card" obtained based on the network connection point of each computer. Querying the IP address allows us to effectively know where the computer is. Location.

How to query the specific geographical location based on the IP address

Baidu Map has an API to query the address based on the IP

http://lbsyun.baidu.com/index.php?title=webapi/ip-api

1. Directly check the IP address according to Baidu Map API

The API says that requesting the following two addresses can obtain the specific address based on the IP:

The request parameters are as follows:

If our request ip is 192.168.1.1, then the request address is constructed as follows:

http://api.map.baidu.com/location/ip?ak=XXX(自己申请的API TOKEN)&ip=192.168.1.1

Python is used as the experimental language here, and the code and comments are as follows:

from urllib import request
import json

# ak is bound with ip
baidu_api_ak = "ZX9QCmwzzItzRO5ssD7GNgEwD4OQduWR"
ip_addr = "xxx.xxx.xxx.xxx"
# Request url
url = "http://api.map.baidu.com/location/ip?ak=" + baidu_api_ak + "&ip=" + ip_addr
req = request.Request(url)
res = request.urlopen(req)
res = res.read()
# Bytes to str
n = res.decode(encoding='utf-8')
# str to json
s = json.loads(n)
t = json.dumps(s, ensure_ascii=False)
print(n)
print(t)

The above code The general meaning is that the request url reads the returned stream and converts it into json format. The result is as shown in the following figure:

Notice that in the above figure, you can only see To a large area such as Chengdu City, Sichuan Province, the fields such as "street" are empty when queried. In other words, only a rough range can be queried.

PS: The returned value also contains latitude and longitude, but according to the original document, these latitude and longitude are the latitude and longitude of the center point of the city where the IP is located.

2. First check the longitude and latitude, and then check the address based on the longitude and latitude.

Based on the IP longitude and latitude search, Baidu Maps does not provide an API, but there are still resources outside the wall. It’s not posted here, go find it yourself. . .

If you get the latitude and longitude, you can query the detailed address through the latitude and longitude Baidu Map API.

The global reverse address encoding API of Baidu Map is as follows:

http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad

As shown below, assume that we have obtained the latitude and longitude. According to the rules on the API, what follows the location parameter is the latitude and longitude. According to the latitude and longitude, we can find the detailed address.

The experimental code and comments are as follows:

#! /usr/bin/env python
# coding=utf-8

from urllib import request
import json

# ak is bound with ip
baidu_api_ak = "ZX9QCmwzzItzRO5ssD7GNgEwD4OQduWR"
url = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=30.6667,104.0670&output=json&pois=1&ak=" + baidu_api_ak
req = request.Request(url)
res = request.urlopen(req)
res = res.read()
n = res.decode(encoding='utf-8')
# The result format is renderReverse&&renderReverse(json str). so....
n = n[len("renderReverse&&renderReverse") + 1:-1]
st = json.loads(n)
# st's structure
#   result: XXXX
#   status: XXXX
result = st["result"]
for i in result:
    print(i, result[i])

The results are as follows:

The address here has been It's very detailed, but there is no detailed analysis and it looks a bit messy. If you want to obtain the specified location information, you can refer to the API.

However, usually the results of such queries are not correct. It may be because the operator keeps users confidential, and the address found may be the address of the operator's server. The specifics have not been studied in detail.

The above is the detailed content of Can the specific address be found based on the IP address?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn