Originally wanted to call Ali's IP interface to query IP attribution. The results found that Ali's interface is not very powerful, mainly inaccurate, but is free of charge and has regional and ISP information. The following is the implementation code
# -*- coding: utf-8 -*-import requestsdef checkip (IP): url = ' http://ip.taobao.com/service/getIpInfo.php ' try: r = requests.get (url, params=ip, timeout=3) except requests. Requestexception as e: print (e) else: json_data = r.json () if json_data[u ' Code '] == 0: print ' Country: ' + json_data[u ' data '][u ' country '].encode (' Utf-8 ') print ' area: ' + json_data[u ' data '][u ' area '].encode (' utf-8 ') print ' Province: ' + json_data[u ' data '][u ' region '].encode (' utf-8 ') print ' City: ' + json_data[u ' Data '][u ' City '].encode (' utf-8 ') print ' owned carrier: ' + json_data[u ' data '][u ' ISP '].encode (' utf-8 ') else: print ' query failed, Please try again later! ' ip={' ip ': ' 202.102.193.68 '}checkip (IP)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/9D/wKiom1SHvnSy5mXdAACUq8a9nqg911.jpg "title=" Ali.jpg "alt=" Wkiom1shvnsy5mxdaacuq8a9nqg911.jpg "/>
But multiple queries found that IP attribution is not accurate, so use 17mon IP query interface. But 17mon cent pay and free library interface, I use the free test, interface returned field is limited, only country, province, city. The code is as follows
# -*- coding: utf-8 -*-import requestsdef lookup (IP): url = ' http://freeipapi.17mon.cn/' + ip try: r = requests.get (URL, timeout=3) except requests. Requestexception as e: print (e) json_data = r.json () print ' Country: ' + json_data[0 '. Encode (' Utf-8 ') print ' province: ' + json_data[1].encode (' utf-8 ') print ' City: ' + json_data[2].encode (' utf-8 ') Return (IP) ip= ' 202.104.15.102 ' lookup (IP)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/9D/wKiom1SHv5Ch3WojAAB9iRfaqo0333.jpg "title=" I17.jpg "alt=" Wkiom1shv5ch3wojaab9irfaqo0333.jpg "/>
Testing is also good, the company to use or choose to buy a paid library query interface bar.
This article from "Lao Xu's Private Cuisine" blog, reproduced please contact the author!
Python to query IP attribution