通过淘宝接口,采集统计访问用户ip对应地区
废话不多说,直接上代码,ip是前一天访问的用户数据存在数据仓库里面的。#!/usr/bin/python# -*- coding:utf-8 -*-# urllib_test.py# author:chunyang.wuimport cx_Oracleimport simplejson as jsonimport urllib2import timedef ha
·
废话不多说,直接上代码,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()
更多推荐
已为社区贡献1条内容
所有评论(0)