您的位置:首页 > 编程语言 > Python开发

将IIS中网站日志批量导入到mysql【python】

2013-03-06 22:26 525 查看
import MySQLdb as mdb

#info = ['', '2012-7-15', '0:00:25', '/index.html', '114.80.102.172', '-', '200', '0']
con = mdb.connect('localhost', 'root', 'root', 'dream')
cursor = con.cursor()

def insertdata(info):
sql = "insert into weblog values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7], info[8])
cursor.execute(sql)

f = open('ex120715.log')

for line in f.readlines():
info = ['']
line = line.strip()
if line[0] == '#':
continue
else:
row = line.strip().split()
data = [row[0], row[1], row[6], row[10], row[12], row[14], row[16], row[19]]
info = info + data
insertdata(info)


使用python的MySQLdb将IIS的网站日志批量导入到mysql中,由于我的网站日志记录了所有的条目,所以数据有点多,用的时候可以自行修改下。

另外,表名是我已经建好的,所以也少了建表的环节。

PS1,不知道咋回事,第一次批量导入的时候出错了。

PS2,导入后就能使用SQL语句查询爬虫数据了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: