python使用在线API查询IP对应的地理位置信息实例
这篇文章中的内容是来源于去年我用美国的VPS搭建博客的初始阶段,那是有很多恶意访问,我就根据access log中的源IP来进行了很多统计,同时我也将访问量最高的恶意访问的源IP拿来查询其地理位置信息。所以,我就用到了根据IP查询地理位置信息的一些东西,现在将这方面积累的一点东西共享出来。
根据IP查询所在地、运营商等信息的一些API如下(根据我有限的一点经验):
1. 淘宝的API(推荐):http://ip.taobao.com/service/getIpInfo.php?ip=110.84.0.129
2. 国外freegeoip.net(推荐):http://freegeoip.net/json/110.84.0.129 这个还提供了经纬度信息(但不一定准)
3. 新浪的API:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=110.84.0.129
4. 腾讯的网页查询:http://ip.qq.com/cgi-bin/searchip?searchip1=110.84.0.129
5. ip.cn的网页:http://www.ip.cn/index.php?ip=110.84.0.129
6. ip-api.com: http://ip-api.com/json/110.84.0.129 (看起来挺不错的,貌似直接返回中文城市信息,文档在 ip-api.com/docs/api:json)
7. http://www.locatorhq.com/ip-to-location-api/documentation.php (这个要注册才能使用,还没用过呢)
(第2个freegeoip.net的网站和IP数据的生成,代码在:https://github.com/fiorix/freegeoip)
为什么其中第4、5两个是网页查询也推荐了呢?是因为两方面原因,一是它们提供的信息比较准,二是使用了页面信息自动抓取(可能会用到我曾经写过的PhantomJS)也容易将其写到程序中成为API。
根据IP查询地理位置信息,我将其写成了一个较为通用的Python库(提供了前面提到的1、2、4、5等4种查询方式的API),可以根据IP查询到地域信息和ISP信息,具体代码见:
https://github.com/smilejay/python/blob/master/py2013/iplocation.py
注意其中对ip.cn网页的解析用到了webdriver和PhantomJS.
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Oct 20, 2013
@summary: geography info about an IP address
@author: Jay
'''
import json, urllib2
import re
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class location_freegeoip():
'''
build the mapping of the ip address and its location.
the geo info is from
'''
def __init__(self, ip):
'''
Constructor of location_freegeoip class
'''
self.ip = ip
self.api_format = 'json'
self.api_url = 'http://freegeoip.net/%s/%s' % (self.api_format, self.ip)
def get_geoinfo(self):
""" get the geo info from the remote API.
return a dict about the location.
"""
urlobj = urllib2.urlopen(self.api_url)
data = urlobj.read()
datadict = json.loads(data, encoding='utf-8')
# print datadict
return datadict
def get_country(self):
key = 'country_name'
datadict = self.get_geoinfo()
return datadict[key]
def get_region(self):
key = 'region_name'
datadict = self.get_geoinfo()
return datadict[key]
def get_city(self):
key = 'city'
datadict = self.get_geoinfo()
return datadict[key]
class location_taobao():
'''
build the mapping of the ip address and its location
the geo info is from Taobao
e.g. http://ip.taobao.com/service/getIpInfo.php?ip=112.111.184.63
The getIpInfo API from Taobao returns a JSON object.
'''
def __init__(self, ip):
self.ip = ip
self.api_url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % self.ip
def get_geoinfo(self):
""" get the geo info from the remote API.
return a dict about the location.
"""
urlobj = urllib2.urlopen(self.api_url)
data = urlobj.read()
datadict = json.loads(data, encoding='utf-8')
# print datadict
return datadict['data']
def get_country(self):
key = u'country'
datadict = self.get_geoinfo()
return datadict[key]
def get_region(self):
key = 'region'
datadict = self.get_geoinfo()
return datadict[key]
def get_city(self):
key = 'city'
datadict = self.get_geoinfo()
return datadict[key]
def get_isp(self):
key = 'isp'
datadict = self.get_geoinfo()
return datadict[key]
class location_qq():
'''
build the mapping of the ip address and its location.
the geo info is from Tencent.
Note: the content of the Tencent's API return page is encoded by 'gb2312'.
e.g. http://ip.qq.com/cgi-bin/searchip?searchip1=112.111.184.64
'''
def __init__(self, ip):
'''
Construction of location_ipdotcn class.
'''
self.ip = ip
self.api_url = 'http://ip.qq.com/cgi-bin/searchip?searchip1=%s' % ip
def get_geoinfo(self):
urlobj = urllib2.urlopen(self.api_url)
data = urlobj.read().decode('gb2312').encode('utf8')
pattern = re.compile(r'该IP所在地为:(.+)')
m = re.search(pattern, data)
if m != None:
return m.group(1).split(' ')
else:
return None
def get_region(self):
return self.get_geoinfo()[0]
def get_isp(self):
return self.get_geoinfo()[1]
class location_ipdotcn():
'''
build the mapping of the ip address and its location.
the geo info is from www.ip.cn
need to use PhantomJS to open the URL to render its JS
'''
def __init__(self, ip):
'''
Construction of location_ipdotcn class.
'''
self.ip = ip
self.api_url = 'http://www.ip.cn/%s' % ip
def get_geoinfo(self):
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/29.0 " )
driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs', desired_capabilities=dcap)
driver.get(self.api_url)
text = driver.find_element_by_xpath('//div[@id="result"]/div/p').text
res = text.split('来自:')[1].split(' ')
driver.quit()
return res
def get_region(self):
return self.get_geoinfo()[0]
def get_isp(self):
return self.get_geoinfo()[1]
if __name__ == '__main__':
ip = '110.84.0.129'
# iploc = location_taobao(ip)
# print iploc.get_geoinfo()
# print iploc.get_country()
# print iploc.get_region()
# print iploc.get_city()
# print iploc.get_isp()
# iploc = location_qq(ip)
iploc = location_ipdotcn(ip)
# iploc.get_geoinfo()
print iploc.get_region()
print iploc.get_isp()

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

手机XML转PDF的速度取决于以下因素:XML结构的复杂性手机硬件配置转换方法(库、算法)代码质量优化手段(选择高效库、优化算法、缓存数据、利用多线程)总体而言,没有绝对的答案,需要根据具体情况进行优化。

不可能直接在手机上用单一应用完成 XML 到 PDF 的转换。需要使用云端服务,通过两步走的方式实现:1. 在云端转换 XML 为 PDF,2. 在手机端访问或下载转换后的 PDF 文件。

C语言中没有内置求和函数,需自行编写。可通过遍历数组并累加元素实现求和:循环版本:使用for循环和数组长度计算求和。指针版本:使用指针指向数组元素,通过自增指针遍历高效求和。动态分配数组版本:动态分配数组并自行管理内存,确保释放已分配内存以防止内存泄漏。

可以将 XML 转换为图像,方法是使用 XSLT 转换器或图像库。XSLT 转换器:使用 XSLT 处理器和样式表,将 XML 转换为图像。图像库:使用 PIL 或 ImageMagick 等库,从 XML 数据创建图像,例如绘制形状和文本。

没有APP可以将所有XML文件转成PDF,因为XML结构灵活多样。XML转PDF的核心是将数据结构转换为页面布局,需要解析XML并生成PDF。常用的方法包括使用Python库(如ElementTree)解析XML,并利用ReportLab库生成PDF。对于复杂XML,可能需要使用XSLT转换结构。性能优化时,考虑使用多线程或多进程,并选择合适的库。

XML格式化工具可以将代码按照规则排版,提高可读性和理解性。选择工具时,要注意自定义能力、对特殊情况的处理、性能和易用性。常用的工具类型包括在线工具、IDE插件和命令行工具。

在手机上高质量地将XML转换成PDF需要:使用无服务器计算平台在云端解析XML并生成PDF。选择高效的XML解析器和PDF生成库。正确处理错误。充分利用云端计算能力,避免在手机上进行繁重任务。根据需求调整复杂度,包括处理复杂的XML结构、生成多页PDF和添加图片。打印日志信息以帮助调试。优化性能,选择高效的解析器和PDF库,并可能使用异步编程或预处理XML数据。确保良好的代码质量和可维护性。

直接在安卓手机上将 XML 转换为 PDF 无法通过自带功能实现。需要通过以下步骤曲线救国:将 XML 数据转换为 PDF 生成器识别的格式(如文本或 HTML);使用 HTML 生成库(如 Flying Saucer)将 HTML 转换为 PDF。
