1234567891011121314151617181920212223242526
#-*-編碼:gbk-*-import URL lib 2 import re try:while True:IP addr = raw _ input(“輸入IP或域名:“)if ipaddr ==““或IP addr = =‘exit“:break else:URL =“/IPS 138 . ASP?ip = % s & ampaction = 2“% IP addr u = URL lib 2 . URL open(URL)s = u . read()# Get IP Address IP = re . find all(r‘\ d { 1,3}\。\d{1,3}\。\d{1,3}\。\ d{1,3 }‘,s)print“\ n * * * * * *以下來自IP138數據庫的結果* * * * *“print“IP Address:“,IP【0】# Get IP Address Location Result = re . find all(r‘(& lt;李& gt。*?& lt/李& gt)‘,s):打印I【4:-5】打印“*“* 45打印“\n”除了:打印“未找到數據”
方法二:本來想調用阿裏的ip接口查詢ip歸屬。原來阿裏的界面很差,主要是不準確,但它是免費的,有地區和ISP信息。以下是實現代碼