思路:ip---->获得经纬度------>利用爬虫去爬取www.gpsspg.com----->得到ip的地址
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # 导入相应模块 import geoip2.database import requests,re def Iplocation(ip): response = geoip2.database.Reader("./GeoLite2-City.mmdb").city(ip) headers = { 'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'Hosts': 'www.gpsspg.com', 'Referer':'http://www.gpsspg.com/iframe/maps/qq_161128.htm?mapi=2', } url = "http://www.gpsspg.com/apis/maps/geo/?output=jsonp&lat=%s000000&lng=%s000000&type=0&callback=jQuery110207785323396673127_1522316918197&_=1522316918198" % (response.location.latitude,response.location.longitude) try: return re.findall("address\"\:\"(.+?)\"", requests.get(url,headers=headers).text)[0] except Exception as e: return e if __name__ == '__main__': ip = "117.87.177.35" print Iplocation(ip)
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/正则/Iplocation.py 江苏省南京市鼓楼区湖南路街道傅厚岗社区南方向 Process finished with exit code 0
http://124.205.69.170/files/9101000005D07809/geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz (GeoLite2-City.mmdb)