一直想做一个通过IP地址获取位置的脚本,无意中发现一个数据包:GeoLite2-City.mmdb,可通过这个包进行位置搜寻,话不多少,开始分享:
1、数据包下载地址:https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
或https://download.csdn.net/download/qq_33267306/21798406?spm=1001.2014.3001.5503
2、通过PYQT5中的designer设计一个界面:
3、将ui文件转换为py文件,界面代码address.py如下:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(510, 471)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.horizontalLayout_2.addWidget(self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout_2.addWidget(self.lineEdit_3)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setObjectName("textBrowser_2")
self.verticalLayout.addWidget(self.textBrowser_2)
self.verticalLayout_2.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 510, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(MainWindow.get_ip)
self.pushButton_2.clicked.connect(MainWindow.get_location)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "位置查询脚本"))
self.label.setText(_translate("MainWindow", "IP地址:"))
self.pushButton.setText(_translate("MainWindow", "查询"))
self.label_2.setText(_translate("MainWindow", "纬度:"))
self.label_3.setText(_translate("MainWindow", "经度:"))
self.pushButton_2.setText(_translate("MainWindow", "查询"))
其中的pushButton按钮链接主代码中的get_ip方法;pushButton_2按钮链接主代码中的get_location方法;
4、写主代码address_APP.py:
import sys
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtGui import QIcon
from address import Ui_MainWindow
import geocoder
import geoip2.database
class Address(QMainWindow,Ui_MainWindow):
def __init__(self,parent=None):
super().__init__(parent)
self.setupUi(self)
def get_ip(self):
try:
reader = geoip2.database.Reader('D:/python/2021/IP地址/GeoLite2-City_20210824/GeoLite2-City.mmdb')
ip = self.lineEdit.text()
data = reader.city(ip)
#读取国家名称
Country_Name = data.country.name
#读取州(国外)/省(国内)名称
Country_SpecificName = data.subdivisions.most_specific.name
#读取城市名称
City_Name = data.city.name
#获取纬度
Location_Latitude = data.location.latitude
#获取经度
Location_Longitude = data.location.longitude
self.textBrowser.setText('IP地址: ' + ip +'\n'
+'国家: ' + Country_Name +'\n'
+'省份: ' + Country_SpecificName+'\n'
+'城市: ' + City_Name +'\n'
+'纬度: ' + str(Location_Latitude) +'\n'
+'经度: ' + str(Location_Longitude))
except Exception as e:
a = str(e)
self.textBrowser.setText(a)
def get_location(self):
try:
latitude = self.lineEdit_2.text()
longitude = self.lineEdit_3.text()
g = geocoder.arcgis([latitude,longitude], method='reverse')
country = g.country
state = g.state
city = g.city
address = g.address
self.textBrowser_2.setText('国家: ' + country +'\n'
+'省份: ' + state +'\n'
+'城市: ' + city +'\n'
+'具体位置: ' + address)
except Exception as e:
a = str(e)
self.textBrowser_2.setText(a)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Address()
ui.show()
sys.exit(app.exec_())
5、运行address_APP.py