查询IP所在地区
使用Python查詢IP所在地區(qū)
直接上代碼
import json import reimport requestsdef search_sp0_baidu_com(ip):"""http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=123.123.123.123&co=&resource_id=6006&oe=utf8"""url = f'http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&co=&resource_id=6006&oe=utf8'try:response = requests.get(url, timeout=5)json_obj = response.json()# json_obj數(shù)據(jù)"""{'status': '0', 't': '', 'set_cache_time': '', 'data': [{'location': '北京市北京市 聯(lián)通', 'titlecont': 'IP地址查詢', 'origip': '123.123.123.123', 'origipquery': '123.123.123.123', 'showlamp': '1', 'showLikeShare': 1, 'shareImage': 1, 'ExtendedLocation': '', 'OriginQuery': '123.123.123.123', 'tplt': 'ip', 'resourceid': '6006', 'fetchkey': '123.123.123.123', 'appinfo': '', 'role_id': 0, 'disp_type': 0}]} """ip_address = json_obj['data'][0]["location"]except Exception as e:return Noneelse:return ip_addressdef search_ip_api_com(ip):"""http://ip-api.com/json/123.123.123.123?lang=zh-CN"""url = f'http://ip-api.com/json/{ip}?lang=zh-CN'try:response = requests.get(url, timeout=5)json_obj = response.json()# json_obj數(shù)據(jù)"""{'status': 'success', 'country': '中國(guó)', 'countryCode': 'CN', 'region': 'BJ', 'regionName': '北京市', 'city': '朝陽(yáng)', 'zip': '', 'lat': 39.9771, 'lon': 116.384, 'timezone': 'Asia/Shanghai', 'isp': 'China Unicom Sichuan Province Network', 'org': '', 'as': 'AS4837 CHINA UNICOM China169 Backbone', 'query': '123.123.123.123'} """ip_address = f"{json_obj['regionName']}{json_obj['city']}"except Exception as e:return Noneelse:return ip_addressdef search_ip_ws_126_net(ip):"""http://ip.ws.126.net/ipquery?ip=123.123.123.123"""url = f'http://ip.ws.126.net/ipquery?ip={ip}'try:response = requests.get(url, timeout=5)text = response.text# text數(shù)據(jù)"""var lo="北京市", lc="北京市";var localAddress={city:"北京市", province:"北京市"}"""city = re.match(r".*city:\"([\u4e00-\u9fa5]+)\".*", text, re.S).group(1)province = re.match(r".*province:\"([\u4e00-\u9fa5]+)\".*", text, re.S).group(1)ip_address = f'{province}{city}'except Exception as e:return Noneelse:return ip_addressdef search_whois_pconline_com_cn(ip):"""http://whois.pconline.com.cn/jsFunction.jsp?callback=jsShow&ip=123.123.123.123"""url = f'http://whois.pconline.com.cn/ipJson.jsp?ip={ip}'try:response = requests.get(url, timeout=5)text = response.text.encode('utf-8')# text數(shù)據(jù)"""if(window.IPCallBack) {IPCallBack({"ip":"123.123.123.123","pro":"北京市","proCode":"110000","city":"北京市","cityCode":"110000","region":"順義區(qū)","regionCode":"110113","addr":"北京市順義區(qū) 聯(lián)通","regionNames":"","err":""});} """json_str = re.match(r".*?{IPCallBack\((.*)\);}.*", text, re.S).group(1)json_obj = json.loads(json_str)ip_address = f"{json_obj['pro']} {json_obj['city']} {json_obj['region']}"except Exception as e:return Noneelse:return ip_addressdef search_ip_address(ip):ip_address = search_sp0_baidu_com(ip)if ip_address is None:ip_address = search_ip_api_com(ip)if ip_address is None:ip_address = search_ip_ws_126_net(ip)if ip_address is None:ip_address = search_whois_pconline_com_cn(ip)return ip_addressif __name__ == '__main__':print(search_ip_address('123.123.123.123'))希望大佬能多多指教
調(diào)用API
如果大家有更多的接口希望能評(píng)論分享
總結(jié)
- 上一篇: 从txt文件逐行读取多个关键字,并在文件
- 下一篇: 华为T8950d和小米手机1对比