用python获取ip信息
1.138网站
http://user.ip138.com/ip/
首次注册后赠送1000次请求,API接口请求格式如下,必须要有token值
1 import httplib2
2 from urllib.parse import urlencode #python3
3 #from urllib import urlencode #python2
4 params = urlencode({'ip':'8.8.8.8','datatype':'jsonp','callback':'find'})
5 url = 'http://api.ip138.com/query/?'+params
6 headers = {"token":"8594766483a2d65d76804906dd1a1c6a"}#token为示例
7 http = httplib2.Http()
8 response, content = http.request(url,'GET',headers=headers)
9 print(content.decode("utf-8"))
响应格式
{
"ret":"ok", // ret 值为 ok 时 返回 data 数据 为err时返回msg数据
"ip":"114.114.114.114", // ip
"data":[
"中国", // 国家
"江苏", // 省会或直辖市
"南京", // 地区或城市
"电信", // 运营商
"210000", // 邮政编码
"025" // 地区区号
]
}
2、ip-api,部分数据不准确,但不限请求次数,且可批量json请求,返回数据可选:json、xml、csv
帮助文档
1 {
2 "status":"success",
3 "country":"美国",
4 "countryCode":"US",
5 "region":"VA",
6 "regionName":"弗吉尼亚州",
7 "city":"Ashburn",
8 "zip":"20149",
9 "lat":39.0438,
10 "lon":-77.4874,
11 "timezone":"America/New_York",
12 "isp":"Level 3",
13 "org":"Google LLC",
14 "as":"AS15169 Google LLC",
15 "query":"8.8.8.8"
16 }
哈哈,上python,获取信息
1 """
2 简化版
3 """
4 import requests
5 lid=input("请输入你要查询的IP:")
6
7 headers = {
8 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
9 url="http://ip-api.com/json/{}?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query&lang=zh-CN".format(lid) #加headers反爬
10 rb=requests.get(url,headers=headers)
11 print(rb.text)
能获取ip信息的,但有点乱,不喜欢,个人有点强迫症,优化一下吧
1 #!/usr/bin/env python3
2 # coding:utf-8
3 # 2019/11/14 14:45
4 #lanxing
5 import requests
6 import time
7
8 strat=time.time()
9 def chax():
10 lid=input("请输入你要查询的IP:")
11 print("**************************************开始获取IP信息**********************************************")
12 head = {
13 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
14 url="http://ip-api.com/json/{}?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,query&lang=zh-CN".format(lid)
15 rb=requests.get(url,headers=head)
16 # gf=BeautifulSoup(rb)
17 # print(rb.text)
18 # print("status:"+rb.content["status"])
19 response = rb.text
20 print(type(response))
21 # print(response)
22 # 把str转换到dic
23 dict_response = eval(response)
24 # for i in dict_response.items():
25 # print(i)
26
27 for key,value in dict_response.items():
28
29 print("IP信息:" + str(key) + " : " + str(value))
30
31
32 chax()
33 end=time.time()
34 print("**************************************大佬,查询IP信息完成!**************************************")
35 print('查询耗时:',end-strat)
这个,理论上没有查询次数限制,有空再搞搞
参考文档:https://www.jianshu.com/p/bd0616fc30b8
https://ip-api.com/docs/api:json#test