# encoding:utf-8
import requests
import socket
def getIP(domain):
"""通过域名获取IP"""
myaddr = socket.getaddrinfo(domain, None)
return myaddr[0][4][0]
def get_ip_info(ip):
"""通过IP获取所在低信息"""
r = requests.get('http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip)
if r.json()['code'] == 0:
i = r.json()['data']
country = i['country'] # 国家
area = i['area'] # 区域
region = i['region'] # 地区
city = i['city'] # 城市
isp = i['isp'] # 运营商
# print(u'国家: %s\n区域: %s\n省份: %s\n城市: %s\n运营商: %s\n' % (country, area, region, city, isp))
return country
else:
print "ERROR! ip: %s" % ip
return None
if __name__ == '__main__':
IP = getIP("www.sqsglj.com") # 注意: 这里输入的域名不包含http及https前缀
country = get_ip_info(IP)
if not country:
country = "无法识别该网站所属地"
print("该网站的所属国家为: %s" % country.encode('utf-8'))
python-通过域名获取该域名所属国家及地区
最新推荐文章于 2023-05-19 13:01:39 发布