Home > Backend Development > Python Tutorial > python实现查询IP地址所在地

python实现查询IP地址所在地

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:23:06
Original
1744 people have browsed it

使用IP138数据库查询域名或IP地址对应的地理位置。

#-*- coding:gbk -*-
import urllib2
import re
 
try:
  while True:
    ipaddr = raw_input("Enter IP Or Domain Name:")
    if ipaddr == "" or ipaddr == 'exit':
      break
    else:
      url = "http://www.ip138.com/ips138.asp?ip=%s&action=2" % ipaddr
      u = urllib2.urlopen(url)
      s = u.read()
      #Get IP Address
      ip = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',s)
      print "\n****** Below Result From IP138 Database *****"
      print "IP Address:",ip[0]
      #Get IP Address Location
      result = re.findall(r'(<li>.*&#63;</li>)',s)
      for i in result:
        print i[4:-5]
      print "*"*45
      print "\n"
 
except:
  print "Not Data Find"
Copy after login

以上所述就是本文的全部内容了希望大家能够喜欢。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template