- import urllib.request
- #jk409 2014-7-31 by python3.4编写用于[批量]查询IP地址国家区域
- def chaxun(ip_addr):
- ip_addr2=ip_addr
- domain_addr='http://ip.taobao.com/service/getIpInfo.php?ip='
- domain=domain_addr+ip_addr2
- res=urllib.request.urlopen(domain).read()
- page=eval(res)
- if page['code']==0:
- ww=page['data']['ip']+' '+page['data']['country_id']+' '+page['data']['country']+page['data']['region']+page['data']['city']
- '''
- print(
- page['data']['ip'],page['data']['country_id'],
- page['data']['country'],page['data']['region'],
- page['data']['city']
- )
- '''
- print(ww)
- wr_f.write(ww+'\\n')
- else:
- print('faile')
- if __name__=="__main__":
- while 1:
- ip_addr = str(input('请输入要查询的IP:'))
- #if ip_addr == "quit":
- # break;
- wr_f=open('c://2.log','w')
- f=open('c://2.txt','r')
- for a in f.readlines():
- chaxun(a.split()[0])
- f.close()
- wr_f.close()
- #该片段来自于http://www.codesnippet.cn/detail/0408201410102.html
来源: http://www.codesnippet.cn/detail/0408201410102.html