发现更大的SEO世界
 找回密码
 注册
搜索
»首页»SEO培训 SEO论坛 SEO交流 帖子
发新帖
GoGo闯,不折腾不舒服斯基    

没赞了。。。分享个日志分析脚本

本帖最后由 GoGo闯 于 2014-6-28 22:40 编辑

猛然发现没有赞了!!!没赞怎么上课呢!!!!!!
----------------------------------------------------------------

分享一个自己平常用的日志分析脚本,很简单,主要统计URL的抓取和流量数据,略细分。光年那宏观的数据感觉没多大用,就没往里加。因为是临时想到什么就往里加的什么,搞得好多变量自己都忘了是干嘛的,所以整体看上去比较繁琐。效率一般,i3处理器1G多日志3、4分钟。

执行命令:python log_file seo_file        
log_file:要输入日志的文件名,seo_file:随便起一个。。。  

脚本输出的内容包括:

蜘蛛抓取量(总抓取量)
每类页面的抓取量(分不同蜘蛛的;包含唯一抓取量和总抓取量,对比看下重复抓取对不多。。。)
SEO流量(统计的搜索引擎在‘seo_traffic_req’变量里,觉得不够在自己加)
每类页面的SEO流量
每类页面的百度SEO流量和360 SEO流量
蜘蛛状态码汇总
百度来源关键词


  1. #coding:utf-8
  2. #weblog analytics

  3. import re
  4. import sys
  5. import urllib
  6. import os

  7. input_file,seo_file = sys.argv[1:3]        #要输入的日志文件名,和输出的seo流量文件名
  8. seo_url = open(seo_file,'a')
  9. #fenci = open(fenci_file,'a')        ps:需要单独输出百度来源关键词文件,则取消注释
  10. baidu_seo = open('baiduseo.txt','a')

  11. #要统计蜘蛛抓取及流量数据的页面url对应的正则,想统计1个就写1个,想统计100个就写100个,根据自己需求替换下~~
  12. mulu_re = [
  13. '/abc/[0-9]+.html',
  14. '/abc/g[0-9]+/[a-z]+.html'
  15. ]

  16. #要统计的蜘蛛,根据自己需求替换下
  17. kz_spider = [
  18. 'Baiduspider.*search/spider.html'        #因为只匹配‘Baiduspider’可能把假蜘蛛也算进来,所以这么写。。
  19. #'360Spider'
  20. #'Googlebot',
  21. #'Sogou'
  22. ]

  23. weblog = open(input_file).read()

  24. word_re = re.compile('\.baidu\.com/.*?(?:wd|word)=(.*?)[&"]')
  25. seo_traffic_req = re.compile(r'(so.com/.*?q=|360.cn/.*?q=|baidu.com/.*wd=|baidu.com/.*word=|so.com/.*q=|sogou.com/.*query=|youdao.com/.*q=|yahoo.com/.*p=|bing.com/.*q=|google.com/.*q=)')
  26. baidu_seo_re = re.compile(r'(baidu.com/.*wd=|baidu.com/.*word=)')

  27. seo_traffic = 0
  28. seo_baidu = 0
  29. pagecode = {}
  30. baidupagecode = {}


  31. def spider_zq(spider):
  32.         req = re.compile(spider)
  33.         data = len(re.findall(req,weblog))
  34.         return data
  35.        
  36. def url_spider_zq(zz,spider):
  37.         url_re = zz + '.*' + spider
  38.         req = re.compile(url_re)
  39.         data_one = len(list(set(re.findall(req,weblog))))  #唯一抓取量
  40.         data_two = len(re.findall(req,weblog))        #总抓取量
  41.         #e = '%.2f%%'% (float('%.1f'%(data_two-data_one))/data_two)   
  42.         return data_one,data_two

  43. print "\n"
  44. print "<-------------------------------每个蜘蛛的总抓取量---------------------------------->"
  45. for spider in kz_spider:
  46.         print spider + "总抓取量:",spider_zq(spider)
  47. print "\n"

  48. print "<-------------------------------蜘蛛目录抓取量---------------------------------->"
  49. for spider in kz_spider:
  50.         print spider+"目录抓取量:","\n"       
  51.         for zz in mulu_re:
  52.                 print zz,":",url_spider_zq(zz,spider)
  53.         print "\n"

  54. print "<-------------------------------SEO总流量---------------------------------->"
  55. for line in open(input_file):
  56.         data = re.search(seo_traffic_req,line)
  57.         baidu = re.search(baidu_seo_re,line)
  58.         if data:
  59.                 seo_traffic += 1
  60.                 seo_url.write(line+'\n')
  61.         else:
  62.                 continue
  63.         if baidu:
  64.                 seo_baidu += 1
  65.                 baidu_seo.write(line+'\n')
  66.         else:
  67.                 continue
  68.         code = line.split(' ')[9]
  69.         if code.isdigit():
  70.                 if code in pagecode:
  71.                         pagecode[code] += 1
  72.                 else:
  73.                         pagecode[code] = 1
  74.                
  75. print 'SEO流量:',seo_traffic,"\n"

  76. baidu_seo.close()
  77. seo_url.close()
  78. seo_mulu = open(seo_file).read()
  79. baiduseo = open('baiduseo.txt').read()                       
  80.                                
  81. print "<-------------------------------SEO目录流量---------------------------------->"
  82. print "网站目录SEO流量统计:","\n"
  83. for line in mulu_re:
  84.         req = re.compile(line)
  85.         seo_data = len(re.findall(req,seo_mulu))
  86.         print line,seo_data

  87. print "\n"
  88. print "<-------------------------------百度 SEO目录流量---------------------------------->"
  89. print "网站目录SEO流量统计:","\n"
  90. for line in mulu_re:
  91.         req = re.compile(line)
  92.         seo_data = len(re.findall(req,baiduseo))
  93.         print line,seo_data

  94. print "\n"
  95. print "<-------------------------------360 SEO目录流量---------------------------------->"
  96. print "360 SEO流量统计:","\n"
  97. for line in mulu_re:
  98.         line_360 = line + ".*(so.com|360.cn)/.*?q="
  99.         req = re.compile(line_360)
  100.         seo_data_360 = len(re.findall(req,seo_mulu))
  101.         print line,seo_data_360
  102.        
  103. print "\n"
  104. print "<-------------------------------蜘蛛状态码---------------------------------->"
  105. pagecode_sort = sorted(pagecode.iteritems(), key=lambda d:d[1], reverse = True)       
  106. print pagecode_sort
  107. print "\n"       
  108.                
  109. os.remove('baiduseo.txt')   
  110. os.remove(seo_file)        #如果需要日志中SEO流量的部分可以删掉这行

  111. #如果需要日志中的百度来源关键词可以取消注释
  112. #for line in open(seo_file):       
  113. #        word = re.search(word_re,line)               
  114. #        if not word:               
  115. #                continue   
  116. #        kw = urllib.unquote_plus(word.group(1))  
  117. #        if 'ie=utf-8' not in line:                          
  118. #                kw = kw.decode('gb2312','ignore').encode('utf-8')
  119. #                fenci.write(kw+"\n")


复制代码

评分

参与人数 4赞同 +16 收起 理由
yxlwfds + 4 很给力!
小小 + 4 直接上传脚本多好
AIR_seoqx + 4 恩,研究一下你的思路
c0901yuan + 4 神马都是浮云

查看全部评分

发表于 2014-6-28 22:36:28
回复 收藏
c0901yuan,一直喜欢 用c0901yuan  做网名。    

最近爱上这个了
发表于 2014-6-29 11:10:24
回复 收藏
快速回复 返回顶部 返回列表