赞
踩
python3—-读取本机谷歌浏览器的网页浏览历史记录sqlit3数据,保存到远程windows server的mysql数据库中
1、配置环境
准备一台windows server 2003服务器,并且安装mysql数据库,也保证可以远程连接数据库,如果连接出错参考:https://www.cnblogs.com/Bighua/p/7629082.html
在Mysql中创建数据库chrome_history,数据表使用Python3脚本创建成表urls
2、本机的谷歌浏览器的历史记录信息
路径为:C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default
查看历史记录的sqlite3数据
3、python脚本:
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 22 20:00:02 2018
@author: Administrator
"""
import os
import sqlite3
import pymysql
def connect_mysql(tempdata):
db_config = {
'host':'192.168.16.115',#远程windows server 2003数据库地址
'port':3306,
'user':'root',
'password':'root',
'db':'chrome_history',#需要连接的数据库chrome_history
'charset':'utf8mb4'#编码
}
try:
cms = pymysql.connect(**db_config)
cur = cms.cursor()#获取一个游标
#对chrome_history库创建表urls数据
#cur.execute("CREATE TABLE urls (id VARCHAR(10), url VARCHAR(500), title VARCHAR(150), visit_count VARCHAR(20), type VARCHAR(2), last_visit_time VARCHAR(20), hidden VARCHAR(2));")
#组装成的可用的mysql语句
insertsql = "insert into urls values("+str(tempdata[0])+","+str(tempdata[1])+","+str(tempdata[2])+","+str(tempdata[3])+","+str(tempdata[4])+","+str(tempdata[5])+","+str(tempdata[6])+");"
new1_insertsql = insertsql.replace(",","','")
new2_insertsql = new1_insertsql.replace("(","('")
new3_insertsql = new2_insertsql.replace(")","')")
print(new3_insertsql)
#清空urls表的所有数据
#cur.execute('delete from urls')#把urls表已经有的数据清空
#cur.execute('flush privileges;')#必须实时更新数据
#在取出列表tempdata最新的一条数据实时插入urls数据表
cur.execute(new3_insertsql)
cur.execute('flush privileges;')#必须实时更新数据
cur.execute('select * from urls;')#查询urls数据表中所有数据
data = cur.fetchall()
#for d in data:#打印查询的urls数据表的数据
#print('id: '+str(d[0])+'\t'+'url: '+str(d[1])+'\t'+'title: '+str(d[2])+'\t'+'visit_count: '+str(d[3])+'\t'+'type: '+str(d[4])+'\t'+'last_visit_time: '+str(d[5])+'\t'+'hidden: '+str(d[6]))
cur.close()
except Exception as e:
print(e)
if __name__ == '__main__':
#指到用户的谷歌浏览器的历史记录
data_path = r'C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default'
files = os.listdir(data_path)#遍历该目录下的文件
history_db = os.path.join(data_path,'history1')#组装绝对路径
tempdata = []
#查询数据库中urls的所有内容
conn = sqlite3.connect(history_db)
cursor = conn.cursor()
select_statement_queryall = 'SELECT * FROM urls;'
cursor.execute(select_statement_queryall)
results = cursor.fetchall()
for d in results:#遍历的读取历史记录的urls表中所有数据
#print("ID: "+str(d[0])+'\t'+"URL: "+str(d[1])+"\t"+"Title: "+str(d[2])+'\t'+"visit_count: "+str(d[3])+'\t'+"typed_count: "+str(d[4])+'\t'+"last_visit_time: "+str(d[5])+'\t'+"hiddlen: "+str(d[6])+'\t')
for i in range(7):
tempdata.append(d[i])#对取到的每一个数据进行加入列表
#print(tempdata)
connect_mysql(tempdata)#把每一次读取到的数据插入mysql数据中
tempdata = []#清空列表,重新接受下一组最新数据
4、脚本运行:
insert into urls values('1','http://baojia.3hk.cn/301','baojia.3hk.cn','0','0','0','0');
insert into urls values('2','http://blog.csdn.net/SKI_12/article/category/6689258','提权 - 闲云~ - CSDN博客','0','0','0','0');
insert into urls values('3','http://blog.csdn.net/SKI_12/article/details/72972238?locationNum=2&fps=1','关于《Python绝技:运用Python成为顶级黑客》的学习笔记 - 闲云~ - CSDN博客','0','0','0','0');
.................................................
.................................................
insert into urls values('254','http://car.bitauto.com/suv/x/','【选车工具|选车中心_汽车车型大全:小型SUV】-易车网','1','0','13174153673198732','0');
insert into urls values('275','http://www.soyunpan.com/','百度云网盘资源搜索引擎_搜云盘!','1','0','13174154423576599','0');
insert into urls values('276','http://www.soyunpan.com/search/%E5%B0%91%E5%84%BF%E6%8A%98%E7%BA%B8-0-%E5%85%A8%E9%83%A8-0.html','少儿折纸 百度云资源网盘下载_搜云盘','1','0','13174154433463515','0');
insert into urls values('277','http://www.soyunpan.com/file/27104757.html','折纸 百度云网盘资源下载_百度云盘','1','0','13174154436053165','0');
5、在windows server 2003服务器的mysql数据库中插入数据成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。