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

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语句查询爬虫数据了。

原文地址:https://www.cnblogs.com/alexkh/p/2947071.html