废话不多说,直接上代码,ip是前一天访问的用户数据存在数据仓库里面的。准确率应该在95%以上

#!/usr/bin/python
# -*- coding:utf-8 -*-
# urllib_test.py
# author:chunyang.wu
 
 
import cx_Oracle
import simplejson as json
import urllib2
import time
 
def handle_ip():
    db=cx_Oracle.connect("user/pass@192.168.0.11:1521/db")
    curs=db.cursor()
    KET_sql="select distinct ip from dm_client_log WHERE event_time>=oracle_to_unix(TRUNC(SYSDATE - INTERVAL '1' DAY)) AND event_time<oracle_to_unix(TRUNC(SYSDATE))"
    curs.execute(KET_sql)
    curs.arraysize = 200
    result=curs.fetchall()
    count = 0
    failed = 0
    for ip in result:
        time.sleep(0.5)
        url = ('http://ip.taobao.com/service/getIpInfo.php?ip=%s') %(ip)
        page = urllib2.urlopen(url,data=None,timeout=10)
        data = page.read()
        ip_dic = json.loads(data)
        for k,v in ip_dic.items():
            if(v == 1):
                print v
                failed +=1
            elif(k == 'data'):
                print v["ip"],v["country"],v["region"],v["city"],count
        count +=1
    print count #,"failed:",failed
 
 
def main():
    p = handle_ip()
 
 
if __name__=="__main__":
    main()


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐