Personal tools
You are here: Home PapaBlog Pythonでアクセス状況をグラフ化...できなかった
今の天気
     23℃ 73% 1015hPa ENE 1m
福岡地方の今の天気
« September 2008 »
Su Mo Tu We Th Fr Sa
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
 
Document Actions

Pythonでアクセス状況をグラフ化...できなかった

by Papasan posted at 2008-03-28 22:36 last modified 2008-03-28 22:36
AWStats

Pythonでアクセス状況をグラフにしてみたいなと思い、色々調べてみると結構色んなパッケージをインストールしないといけない。

PythonとPILだけで何とかできないかとも思ったが、これもまた難しそう。

当サイトではズート前からAWStats を使用してきました。

Pythonのスクリプトで、このAWStatsを読み込んで必要な部分のみHTMLを切抜きました、で、それをトップページにペッタンコと貼り付けました。当分これで我慢しましょ。

awstats.py:

 #!/usr/bin/env python
 # coding: utf-8

 import socket
 import re
 import string
 import urllib2

 flags = re.I | re.M | re.S
 pat_daysofmonth = re.compile('<a name="daysofmonth">&nbsp;</a><br />(.*?)<br />.<table>', flags)
 pat_table = re.compile('<center>.(.*?)end_of_html', flags)
 url = 'http://localhost/awstats/awstats.pl?config=papasan.org&framename=mainright'

 def get_awstats_html():
     body = get_site(url)
     body = get_first_match(pat_daysofmonth, body)
     body += 'end_of_html'
     body = get_first_match(pat_table, body)
     body = string.replace(body, '<img align="bottom"', '<img style="vertical-align: bottom;"')
     body = string.replace(body, 'src="/awstatsicons/other/', 'src="awstats/')
     return body

 def get_site(url):
     socket.setdefaulttimeout(120)
     request = urllib2.Request(url)
     request.add_header("User-Agent","AWStats Scanner 1.0")
     opener = urllib2.build_opener()
     body = opener.open(request).read()
     return body

 def get_first_match(pat,body):
     m = re.search(pat,body,flags)
     if m and m.groups():
         return m.groups()[0]
     else:
         return ""

The URL to Trackback this entry is:
http://papasan.org/papablog/512/tbping

(トラックバック元のページには"Papasan"や"papasan"または言及リンクの記述が必要です。)

Add comment

You can add a comment by filling out the form below. Plain text formatting.

(Required)
(Required)
(Required)
This helps us prevent automated spamming.
スパム防止にご協力を!!

画像の文字を入力して下さい。