2008/03/28
Pythonでアクセス状況をグラフ化...できなかった
by
Papasan
—
posted at
2008-03-28 22:33
last modified
2008-03-28 22:36
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"> </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 ""
コメントパーサー
RSS配信